我正在学习如何使用python 3.8中的BeautifulSoup抓取网络,我遇到了一个找不到解决方案的问题。
我正在尝试从此页面获取每种产品的品牌:
https://www.linio.com.mx/c/computacion/pc-portatil
有68种产品,而我只能得到60种,我确定我的脚本没有抢到第一款和最后一款产品,分别是ASUS A540和Lenovo Ideapad L340。
这是我的剧本
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
my_url = 'https://www.linio.com.mx/c/computacion/pc-portatil'
uClient = urlopen(my_url)
page_html = uClient.read()
uClient.close()
soup_page = soup(page_html, 'html.parser')
containers = soup_page.find(id="catalogue-product-container").findAll(True, recursive=False)
for container in containers:
try:
print(container.a.find(itemprop="brand")["content"])
except TypeError:
pass
在我看来,HTML解析器并不是完美的,有些人面临着与您相似的问题-Beautiful Soup findAll并没有全部找到他们。
我运行了您的代码,但遇到了同样的问题。该错误已经在'find'和'findAll'方法上发生过-根据首页,似乎出于某种未知原因,我只提取了赞助产品。
您是否尝试过硒?根据我的经验,与其他Web抓取库相比,Selenium为我减少了麻烦,并为我节省了很多时间。但那只是我的个人意见。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句