用beautifulsoup解析问题

时间:2011-08-31 21:07:01

标签: python parsing beautifulsoup

我正在尝试解析以下网页link。 代码如下:

import urllib2
import sys
from BeautifulSoup import BeautifulSoup

url = 'http://www.etsy.com/teams/list'
source = urllib2.urlopen(url)

soup = BeautifulSoup(source)
print soup.prettify()

print len(soup('h3')) #to print the no of occurances of h3 
h3s = soup.findAll('h3') #finding the same as above
print len(h3s)

问题是,它打印1.而网页包含至少10'h3'。我无法弄清问题在哪里 我正在使用python 2.7和BeautifulSoup 3.0.7

1 个答案:

答案 0 :(得分:2)

我建议改为使用lxml

>>> import lxml.html
>>> doc = lxml.html.parse('http://www.etsy.com/teams/list')
>>> len(doc.xpath('//h3'))
<<< 10