分析HTML时出现Python BeautifulSoup错误
我在BeautifulSoup解析来自的某些信息时遇到问题。在我使用模块Mechanize获取html代码之后,我正试图将我想要的最终结果集中在这个html小片段上:分析HTML时出现Python BeautifulSoup错误,python,html,python-2.7,beautifulsoup,mechanize,Python,Html,Python 2.7,Beautifulsoup,Mechanize,我在BeautifulSoup解析来自的某些信息时遇到问题。在我使用模块Mechanize获取html代码之后,我正试图将我想要的最终结果集中在这个html小片段上: <table style="padding-top:10px;"> <tr><th>ISP:</th><td>Brighthouse Networks</td></tr> <tr><th&g
<table style="padding-top:10px;">
<tr><th>ISP:</th><td>Brighthouse Networks</td></tr>
<tr><th>Services:</th><td><a href="/ip-services">None Detected</a></td></tr>
<tr><th>City:</th><td>Miami</td></tr>
<th>Region:</th><td>Florida</td>
<tr><th>Country:</th><td>United States</td></tr>
</table>
我编写的脚本的分析部分是:
soup = BeautifulSoup(html)
table = soup.findall('table',{'style':'padding-top:10px;'})
for t in table:
print t.text
然而,这并没有产生我上面列出的我想要的结果。非常感谢您的帮助。谢谢
table = soup.find_all('table',attrs={'style':'padding-top:10px;'})
我们应该做到这一点
签名如下:
find_all(名称、属性、递归、文本、限制、**kwargs)
若您将属性作为第二个参数传递,find_all希望它是字符串而不是字典。如果您想传递属性字典来查找所有内容,您应该通过将其传递为来完成 这是有效的(:
你的脚本产生了什么结果?你的回答中没有考虑到
地区:佛罗里达
你的beautifulsou导入beautifulsou
的语法错误,请参见编辑是的,你是对的…我错误地拒绝了编辑。。。
table = soup.find_all('table',attrs={'style':'padding-top:10px;'})
from bs4 import BeautifulSoup
html = """<table style="padding-top:10px;">
<tr><th>ISP:</th><td>Brighthouse Networks</td></tr>
<tr><th>Services:</th><td><a href="/ip-services">None Detected</a></td></tr>
<tr><th>City:</th><td>Miami</td></tr>
<th>Region:</th><td>Florida</td>
<tr><th>Country:</th><td>United States</td></tr>
</table>"""
soup = BeautifulSoup(html)
table = soup.findAll('table', {"style":"padding-top:10px;"})[0]
trs = table('tr')
for tr in trs:
print tr.th.text,
print tr.td.text
#and this for the 'Region'
print table("th")[3].text,
print table("td")[3].text
ISP: Brighthouse Networks
Services: None Detected
City: Miami
Country: United States
Region: Florida