首页 > 解决方案 > BeautifulSoup 运动刮刀返回空列表

问题描述

我正在尝试使用 Python 的 BeautifulSoup从这个网站上抓取网球比赛的结果。我已经尝试了很多东西,但我总是得到一个空列表。我犯了一个明显的错误吗?当我检查它时,网站上有这个类的多个实例,但它似乎没有找到它。

import requests
from bs4 import BeautifulSoup

url = 'https://www.flashscore.com/tennis/atp-singles/french-open/results/'

page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')

match_container = soup.find_all('div', class_='event__match event__match--static event__match--last event__match--twoLine')
print(match_container)

标签: pythonweb-scrapingbeautifulsoup

解决方案


乐谱数据被动态拉入页面,您只获得带有请求的初始 HTML。

正如用户 70 在评论中建议的那样,这样做的方法是首先使用像 Selenium 这样的工具,这样您就可以获得在 Web 浏览器的检查工具中看到的所有动态内容。

网上很少有指南展示它是如何工作的——你可以从这个开始:

https://medium.com/ymedialabs-innovation/web-scraping-using-beautiful-soup-and-selenium-for-dynamic-page-2f8ad15efe25


推荐阅读