I'm trying to parse the following web page link. Code below:
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
pr开发者_开发百科int len(h3s)
The problem is, it prints 1. while the web page contains atleast 10 'h3'.I couldn't figure out where the problem lies I am using python 2.7 and BeautifulSoup 3.0.7
I'd recommend using lxml
instead:
>>> import lxml.html
>>> doc = lxml.html.parse('http://www.etsy.com/teams/list')
>>> len(doc.xpath('//h3'))
<<< 10
精彩评论