我正在尝试解析购物网站上的视频游戏标题列表。然而,由于项目列表全部存储在标签内。
This http://www.crummy.com/software/BeautifulSoup/documentation.html#Improving%20Performance%20by%20Parsing%20Only%20Part%20of%20the%20Document文档的部分据说解释了如何仅解析文档的一部分,但我无法解决。我的代码:
from BeautifulSoup import BeautifulSoup
import urllib
import re
url = "Some Shopping Site"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
for a in soup.findAll('a',{'title':re.compile('.+') }):
print a.string
目前打印任何具有非空标题引用的标签内的字符串。但它也对侧栏中的“特价”商品进行了定价。如果我只能拿产品列表div,我会一石二鸟。
非常感谢。
天哪,我很傻,我正在搜索属性 id = products 的标签,但它应该是product_list
如果有人来搜索的话,这是最终的代码。
from BeautifulSoup import BeautifulSoup, SoupStrainer
import urllib
import re
start = time.clock()
url = "http://someplace.com"
html = urllib.urlopen(url).read()
product = SoupStrainer('div',{'id': 'products_list'})
soup = BeautifulSoup(html,parseOnlyThese=product)
for a in soup.findAll('a',{'title':re.compile('.+') }):
print a.string
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)