对于一个有趣的网络抓取项目,我想从 ttps://www.nhl.com/stats/teams 收集 NHL 数据。
有一个可点击的 Excel 导出标签,我可以使用 selenium 和 bs4 找到它。
不幸的是,事情到这里就结束了:
由于没有 href 属性,我似乎无法访问数据。
我通过使用 pynput 模拟鼠标点击得到了我想要的,但我想知道:
我可以采取不同的做法吗?如果感觉很笨拙。
-> 带有导出图标的标签可以在这里找到:
a class="styles__ExportIcon-sc-16o6kz0-0 dIDMgQ"
-> 这是我的代码
`import pynput
from pynput.mouse import Button, Controller
import time
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome(executable_path = 'somepath\chromedriver.exe')
URL = 'https://www.nhl.com/stats/teams'
driver.get(URL)
html = driver.page_source  # DOM with JavaScript execution complete
soup = BeautifulSoup(html)
body = soup.find('body')
print(body.prettify())
mouse = Controller()
time.sleep(5) # Sleep for 5 seconds until page is loaded
mouse.position = (1204, 669) # thats where the icon is on my screen
mouse.click(Button.left, 1) # executes download`            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
没有
href属性,通过JS触发下载。使用selenium时找到您的元素并使用.click()下载文件:在这里使用
css 选择器来获取直接子级的<a>styles__ExportIcon开头的类直接选择它:示例
您可能需要处理 onetrust 横幅,因此请先单击它,然后下载该表。