gpt4 book ai didi

python - 如何使用 Python 从网站下载所有 Zip 文件

转载 作者:行者123 更新时间:2023-12-05 07:41:22 25 4
gpt4 key购买 nike

我正在尝试从这里下载所有压缩文件:https://www.google.com/googlebooks/uspto-patents-grants-text.html网页。

恕我直言,我不是专业的程序员,所以如果我犯了一些愚蠢的错误,请原谅我。

这是我的代码:

from bs4 import BeautifulSoup            
import requests

url = "https://www.google.com/googlebooks/uspto-patents-grants-text.html"
html = requests.get(url)
soup = BeautifulSoup(html.text, "html.parser")

for link in soup.find_all('a', href=True):
href = link['href']

if any(href.endswith(x) for x in ['.zip']):
#if any(href.endswith('.zip')):
print("Downloading '{}'".format(href))
remote_file = requests.get(url + href)

with open(href, 'wb') as f:
for chunk in remote_file.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)

运行代码时出现的错误是:文件“C:/Users/#USER#/#FILEPATH#/Python/patentzipscraper2.py”,第 16 行,位于 用 open(href, 'wb') 作为 f:OSError:[Errno 22] 无效参数:http://storage.googleapis.com/patents/grant_full_text/2015/ipg150106.zip '

但是,当我在浏览器中输入该地址时,我可以下载压缩文件。我猜这与压缩文件的格式有关,我不一定能直接下载/打开它们,但我不确定是什么。我以此为基础的代码是下载你可以直接下载的文件(比如 .txt)

如能提供有关如何下载这些 zip 的任何帮助,我们将不胜感激。

最佳答案

在您的代码中实现如下内容:

import urllib

archive = urllib.request.URLopener()
archive.retrieve("http://yoursite.com/file.zip", "file.zip")

关于python - 如何使用 Python 从网站下载所有 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355760/

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