【问题标题】:BeautifulSoup not finding all tags when using .find method?BeautifulSoup 在使用 .find 方法时找不到所有标签?
【发布时间】:2020-03-06 18:37:16
【问题描述】:

我正在尝试从https://github.com/trending 中获取在 Python 中使用 BeautifulSoup 的趋势存储库的数量。该代码应该找到所有带有 class_ = "Box-row" 的标签,然后打印找到的数字。在该网站上,趋势存储库的实际数量为 25,但代码仅返回 9。

我尝试将解析器从“html.parser”更改为“lxml”,但都返回了相同的结果。

page = requests.get('https://github.com/trending')
soup = BeautifulSoup(page.text, 'html.parser')

soup = BeautifulSoup(page.text)
repo = soup.find(class_ = "Box-row")
print(len(repo))

在 html 中有 25 个带有“Box-row”类属性的标签,所以我希望看到 print(len(repo)) = 25,但实际上是 9。

【问题讨论】:

    标签: web-scraping beautifulsoup


    【解决方案1】:

    试试这个:

    repo = soup.find_all("article",{"class":"Box-row"})
    

    【讨论】:

    • 我刚刚意识到,您也可以使用您的代码。只需使用find_all 而不是find
    • 你还有一行你可能不需要的额外代码soup = BeautifulSoup(page.text)。不知道是你的错字还是什么。
    猜你喜欢
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多