繁体   English   中英

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

[英]Download latest version of file from website to specific location using Python

我正在使用Python(V3.6.6,x64版本)学习硒和网络抓取。 我试图写一个脚本,将执行时自动下载最新的win64的版本geckodriver (v0.22.0,在发布此问题的时间)从URL https://github.com/mozilla/geckodriver/releases到Windows PC上的特定位置。

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

1)下载最新的geckodriver的win64版本

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM