[英]Checking children for tags in Beautiful Soup 4 with python
I am using BeautifulSoup 4 with python to parse through some HTML.我正在使用 BeautifulSoup 4 和 python 来解析一些 HTML。 Here's the code:这是代码:
from bs4 import BeautifulSoup as bs
html_doc = '<p class="line-spacing-double" align="center">IN <i>THE </i><b>DISTRICT</b> COURT OF {county} COUNTY\nSTATE OF OKLAHOMA</p>'
soup = bs(html_doc, 'html.parser')
para = soup.p
for child in soup.p.children:
print (child)
The result is:结果是:
IN
<i>THE </i>
<b>DISTRICT</b>
COURT OF {county} COUNTY
STATE OF OKLAHOMA
This all makes sense.这都是有道理的。 What I'm trying to do is iterate through the results and if I find a <i>
or <b>
then do something different with them.我想要做的是遍历结果,如果我找到<i>
或<b>
然后对它们做一些不同的事情。 When I try the following, it doesn't work:当我尝试以下操作时,它不起作用:
for child in soup.p.children:
if child.findChildren('i'):
print('italics found')
The error is because the first returned child is a string and I'm trying to search it for a child tag and BS4 already knows there are no children present.错误是因为第一个返回的孩子是一个字符串,我正在尝试搜索它的孩子标签,而 BS4 已经知道没有孩子在场。
So I changed up the code to check if the child is a string, and if so, do not attempt to take any action on it, just print it out.所以我修改了代码来检查孩子是否是一个字符串,如果是,不要试图对它采取任何行动,只需将其打印出来。
for child in soup.p.children:
if isinstance(child, str):
print(child)
elif child.findAll('i'):
for tag in child.findAll('i'):
print(tag)
The result of this latest code:最新代码的结果:
IN
COURT OF {county} COUNTY
STATE OF OKLAHOMA
As I loop through the results, I need to be able to check for tags in the result, but I can't seem to figure out how.当我遍历结果时,我需要能够检查结果中的标签,但我似乎无法弄清楚如何。 I thought it should be simple, but I'm stumped.我认为这应该很简单,但我很难过。
EDIT:编辑:
In response to jacalvo:回应 jacalvo:
If I run如果我跑
for child in soup.p.children:
if child.find('i'):
print(child)
It still fails to print out the 2nd and 3rd lines from the HTML code它仍然无法从 HTML 代码中打印出第 2 行和第 3 行
Edit:编辑:
for child in soup.p.children:
if isinstance(child, str):
print(child)
else:
print(child.findChildren('i', recursive=False))
This resulted in:这导致:
IN
[]
[]
COURT OF {county} COUNTY
STATE OF OKLAHOMA
Is this an example what your are trying to do as an example of "do something different" with tags?这是您尝试使用标签“做一些不同的事情”的例子吗? Having a sample of the full desired output in the question would help:在问题中获取完整的所需输出样本将有所帮助:
from bs4 import BeautifulSoup as bs
html_doc = '<p class="line-spacing-double" align="center">IN <i>THE</i> <b>DISTRICT</b> COURT OF {county} COUNTY\nSTATE OF OKLAHOMA</p>'
soup = bs(html_doc, 'html.parser')
para = soup.p
for child in para.children:
if child.name == 'i':
print(f'*{child.text}*',end='')
elif child.name == 'b':
print(f'**{child.text}**',end='')
else:
print(child,end='')
Output:输出:
IN *THE* **DISTRICT** COURT OF {county} COUNTY
STATE OF OKLAHOMA
from bs4 import BeautifulSoup as bs
html_doc = '<p class="line-spacing-double" align="center">IN <i>THE </i><b>DISTRICT</b> COURT OF {county} ' \
'COUNTY\nSTATE OF OKLAHOMA</p> '
soup = bs(html_doc, 'html.parser')
paragraph = soup.p
# all tags dynamically gotten
tags = [tag.name for tag in soup.find_all()]
for child in paragraph.children:
if child.name in tags:
print('{0}'.format(child)) # or child.text
else:
print(child)
Output输出
IN
<i>THE </i>
<b>DISTRICT</b>
COURT OF {county} COUNTY
STATE OF OKLAHOMA
Use findChildren
() and then check the child name with if conditions.使用findChildren
() 然后使用 if 条件检查子名称。
from bs4 import BeautifulSoup as bs
html_doc = '<p class="line-spacing-double" align="center">IN <i>THE </i><b>DISTRICT</b> COURT OF {county} COUNTY\nSTATE OF OKLAHOMA</p>'
soup = bs(html_doc, 'html.parser')
for child in soup.find('p').findChildren(recursive=False) :
if child.name=='i':
print(child)
if child.name=='b':
print(child)
<i>THE </i>
<b>DISTRICT</b>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.