【发布时间】:2014-09-18 06:23:34
【问题描述】:
我正在尝试从此页面获取所有标题的类别。
from bs4 import BeautifulSoup
import urllib2
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) \
AppleWebKit/537.36 (KHTML, like Gecko) \
Ubuntu Chromium/33.0.1750.152 Chrome/33.0.1750.152 Safari/537.36'
}
category_url = ''
html = urllib2.urlopen(urllib2.Request(category_url, None, headers)).read()
page = BeautifulSoup(html)
results = page.find('div', {'class': "results"}).find_all('li')
for res in results:
category = res.find(attrs={'class': "category"}) or res.find(attrs={'class': "categories"})
#print category #till here, I'm getting correct data
print category.b.decompose() #here is the problem? I should get the div element without <b> tag but it returns None
我收到的是None,而不是更新的 dom。
PS:如果您有任何改进此代码的建议,请告诉我。我很乐意进行更改以获得更好的性能和 Python 代码。
【问题讨论】:
标签: python python-2.7 python-3.x beautifulsoup lxml