我开始使用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,以便它变得容易解析?
这只适用于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