【问题标题】:BeautifulSoup is not findign all the tagsBeautifulSoup 没有找到所有的标签
【发布时间】:2021-02-11 12:23:30
【问题描述】:

我正在学习如何在 python 3.8 中使用 BeautifulSoup 抓取网页,但我遇到了一个无法找到解决方案的问题。

我正在尝试从此页面获取每种产品的品牌:

https://www.linio.com.mx/c/computacion/pc-portatil

有 68 个产品,我只得到 60 个,我确定我的脚本没有抓住第一个和最后一个产品,分别是华硕 A540 和联想 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

【问题讨论】:

    标签: python beautifulsoup tags


    【解决方案1】:

    在我看来,HTML 解析器并不完美,有些人面临与您类似的问题 - Beautiful Soup findAll doesn't find them all

    我运行了您的代码,但遇到了同样的问题。该错误已经发生在 'find' 和 'findAll' 方法中 -- 根据第一页,我似乎出于某种未知原因没有提取赞助产品

    您是否尝试过 Selenium?根据我的经验,与其他网络抓取库相比,Selenium 给我带来的问题更少,并且为我节省了很多时间。但那只是我的个人意见。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2015-02-05
      相关资源
      最近更新 更多