使用beautifulsoup解析HTML页面
我开始使用beautifulsoup解析HTML。
用于例如,对于网站的“http://en.wikipedia.org/wiki/PLCB1”使用beautifulsoup解析HTML页面
import sys
sys.setrecursionlimit(10000)
import urllib2, sys
from BeautifulSoup import BeautifulSoup
site= "http://en.wikipedia.org/wiki/PLCB1"
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
table = soup.find('table', {'class':'infobox'})
#print table
rows = table.findAll("th")
for x in rows:
print "x - ", x.string
我得到的输出为无在日那里是URL某些情况下。为什么是这样?
输出:
x - Phospholipase C, beta 1 (phosphoinositide-specific)
x - Identifiers
x - None
x - External IDs
x - None
x - None
x - Molecular function
x - Cellular component
x - Biological process
x - RNA expression pattern
x - Orthologs
x - Species
x - None
x - None
x - None
x - RefSeq (mRNA)
x - RefSeq (protein)
x - Location (UCSC)
x - None
例如,地点后,还有一个个包含“考研搜索”,但显示为无。我想知道它为什么发生。
and
第二:有没有办法在字典中获取th和各自的td,以便它变得容易解析?
Element.string
只有当文本直接位于元素中时才包含值。不包括嵌套元素。
如果使用BeautifulSoup 4,使用Element.stripped_strings
代替:
print ''.join(x.stripped_strings)
对于BeautifulSoup 3,你需要搜索所有文本元素:
print ''.join([unicode(t).strip() for t in x.findAll(text=True)])
如果你想结合<th>
和<td>
元素合并到一个字典中,您可以遍历所有<th>
元素,然后使用.findNextSibling()
来查找相应的<td>
元素,并将它合并上述.findAll(text=True)
招打造自己的字典:
info = {}
rows = table.findAll("th")
for headercell in rows:
valuecell = headercell.findNextSibling('td')
if valuecell is None:
continue
header = ''.join([unicode(t).strip() for t in headercell.findAll(text=True)])
value = ''.join([unicode(t).strip() for t in valuecell.findAll(text=True)])
info[header] = value
如果检查HTML,
<th colspan="4" style="text-align:center; background-color: #ddd">Identifiers</th>
</tr>
<tr class="">
<th style="background-color: #c3fdb8"><a href="/wiki/Human_Genome_Organisation" title="Human Genome Organisation">Symbols</a></th>
<td colspan="3" class="" style="background-color: #eee"><span class="plainlinks"><a rel="nofollow" class="external text" href="http://www.genenames.org/data/hgnc_data.php?hgnc_id=15917">PLCB1</a>; EIEE12; PI-PLC; PLC-154; PLC-I; PLC154; PLCB1A; PLCB1B</span></td>
</tr>
<tr class="">
<th style="background-color: #c3fdb8">External IDs</th>
你会看到在Identifiers
和External IDs
之间有一个<th>
标签,没有文字,只有<a>
标签:
<th style="background-color: #c3fdb8"><a href="/wiki/Human_Genome_Organisation" title="Human Genome Organisation">Symbols</a></th>
这<th>
有没有T分机。所以x.string
是None
。
当然'x.string'是None,但是你如何解决这个问题? :-P – 2013-02-16 14:52:13
@MartijnPieters:我来说说这个,但你回答得太快:) – unutbu 2013-02-16 14:53:14
怎么样最后的情况下有
- 1. 使用BeautifulSoup解析HTML
- 2. Python:用BeautifulSoup解析HTML
- 3. 用BeautifulSoup解析HTML表格
- 4. 解析HTML页面
- 5. 解析html页面
- 6. 使用beautifulsoup解析python中的html
- 7. 在Python中使用BeautifulSoup解析HTML
- 8. 使用BeautifulSoup解析HTML标签
- 9. 解析使用BeautifulSoup
- 10. BeautifulSoup无法解析YouTube页面
- 11. 如何使用PHP解析HTML页面?
- 12. 如何使用beautifulsoup刮从HTML页面
- 13. 的Python - BeautifulSoup - HTML解析
- 14. Python和BeautifulSoup解析HTML
- 15. JSP/HTML页面解析
- 16. 从tcl解析html页面
- 17. 已解析页面的HTML
- 18. *更新:如何用python/beautifulsoup解析html
- 19. 解析HTML和使用Beautifulsoup写入CSV - AttributeError的或没有HTML被解析
- 20. 用robobrowser和beautifulsoup解析网页
- 21. 解析外部HTML页面用PHP
- 22. BeautifulSoup(Python)和解析HTML表格
- 23. BeautifulSoup Library的HTML解析问题
- 24. 从BeautifulSoup解析HTML中删除标签
- 25. 的Python/BeautifulSoup解析HTML馏分
- 26. beautifulsoup解析html标记异常
- 27. beautifulsoup解析html文件内容
- 28. 如何使用BeautifulSoup解析此HTML代码?
- 29. Python,BeautifulSoup或LXML - 使用CSS标记从HTML解析图像URL
- 30. 使用BeautifulSoup解析HTML,但卡在创建BeatuifulSoup对象
这只适用于bs4。相反,@sam可能会使用较早版本的BeautifulSoup。 (不是我-1顺便说一句) – unutbu 2013-02-16 14:48:18
@unutbu:bugger ..更新为包括一个BS3选项 – 2013-02-16 14:48:37
它给TypeError – sam 2013-02-16 14:49:54