我正在学习如何使用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
答案 0 :(得分:0)
在我看来,HTML解析器并不完美,并且有人面临着与您类似的问题-Beautiful Soup findAll doesn't find them all。
我运行了您的代码,但遇到了同样的问题。该错误已经发生在'find'和'findAll'方法上-根据首页,出于某些未知原因,看来仅赞助产品未提取。
您是否尝试过硒?根据我的经验,与其他Web抓取库相比,Selenium为我减少了麻烦,并为我节省了很多时间。但那只是我的个人意见。 :)