我在每个div中有两个输入元素(自闭标签),但我只获得一个输入元素。
这是我的代码:
from bs4 import BeautifulSoup
from bs4.element import Tag
html_doc = '''<cfform>
<div name="first_div">
First name:<cfinput type="text" name="firstname">
Last name:<cfinput type="text" name="lastname">
</div>
<div name="second_div">
Address:<cfinput type="text" name="address">
Contact Number:<cfinput type="text" name="contact_number">
</div>
</cfform>'''
soup = BeautifulSoup(html_doc, 'xml')
for div in soup.find_all("div"):
print(div.get("name"))
for child in div.children:
if isinstance(child, Tag):
print(" ", child.get("name"))
答案 0 :(得分:0)
CCO完全正确。如果您修改了<cfinput>
代码以表明它们是自动关闭的,那么通过添加/
<cfinput/>
,您将获得我们想要查看的输出。
您的代码与CCO的建议:
from bs4 import BeautifulSoup
from bs4.element import Tag
html_doc = '''<cfform>
<div name="first_div">
First name:<cfinput type="text" name="firstname"/>
Last name:<cfinput type="text" name="lastname"/>
</div>
<div name="second_div">
Address:<cfinput type="text" name="address"/>
Contact Number:<cfinput type="text" name="contact_number"/>
</div>
</cfform>'''
soup = BeautifulSoup(html_doc, 'xml')
for div in soup.find_all("div"):
print(div.get("name"))
for child in div.children:
if isinstance(child, Tag):
print(" ", child.get("name"))
运行上述后的输出:
first_div
('',你'第一名')
('',u'lastname')
second_div
('',u'address')
('',u'contact_number')