gpt4 book ai didi

python - 使用 Python 将最新版本的文件从网站下载到特定位置

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:38 25 4
gpt4 key购买 nike

我正在学习 Python 中的 selenium 和 web-scraping(V3.6.6,x64 版本)。我正在尝试编写一个脚本,该脚本在执行时会自动从以下位置下载最新可用的 win64 版本的 geckodriver (v0.22.0,在发布此问题时) url https://github.com/mozilla/geckodriver/releases 到我的 Windows PC 上的特定位置。

我的问题是,当我使用 Mozilla Firefox 浏览器查看页面源时,我尝试下载的特定版本的 ID 和类与所有其他可用版本相同。我无法过滤掉特定部分并获取 href 以便可以下载文件。我肯定错过了一些东西,但尽管进行了多次互联网搜索,我还是无法弄清楚我做错了什么。我请求 Stackoverflow 的专家指导/纠正我接下来的步骤。以下是我试图解决的问题:

1) 下载win64版本的最新geckodriver

2) 文件应下载到 C:\Python

3)如何判断程序已经下载完文件以便进一步执行?

from urllib.request import urlopen, urlretrieve
from bs4 import BeautifulSoup

# Define page where geckodriver can be downloaded
url = "https://github.com/mozilla/geckodriver/releases"

try:
# Query the website and return the html to the variable ‘page’
page = urlopen(url)
except:
# Thow message for any unexpected behaviour when loading page
print("Unable to download geckodriver. Hit any key to exit program.")
user_input = input()
exit()

# Parse the html using beautifulsoup and store in variable `soup`
soup = BeautifulSoup(page, "html.parser")

# Trying to search and filter latest win64 version
result = soup.find_all('a', {'class': 'd-flex flex-items-center'})

最佳答案

首先找到最新版本,然后获取win64链接:

latest = soup.find('div', {'class': 'release-entry'})
results = latest.find_all('a', {'class': 'd-flex flex-items-center'})
for result in results:
if 'geckodriver/releases/download/' in result.get('href) and 'win64.zip' in result.get('href):
print (result.get('href))

关于python - 使用 Python 将最新版本的文件从网站下载到特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52372760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com