首页 > 解决方案 > BeautifulSoup 在使用 .find 方法时找不到所有标签?

问题描述

我正在尝试从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-scrapingbeautifulsoup

解决方案


尝试这个:

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

推荐阅读