[英]How can I add a header to urllib.request.urlretrieve keeping my variables?
I'm trying to download a file from a website but it looks like it is detecting urllib and doesn't allow it to download (I'm getting the error "urllib.error.HTTPError: HTTP Error 403: Forbidden"). 我正在尝试从网站下载文件,但看起来它正在检测urllib,并且不允许下载(我收到错误“ urllib.error.HTTPError:HTTP错误403:禁止”)。
How can I fix this? 我怎样才能解决这个问题? I found on the internet that I had to add a header but the answers weren't going the way I need (It was using Request and I didn't find anything about an argument to add in urllib.request.urlretrieve() for a header)
我在互联网上发现必须添加标头,但答案并没有达到我所需要的方式(它使用的是Request,但我没有找到关于要添加到urllib.request.urlretrieve()中的参数的任何信息标头)
I'm using Python 3.6 我正在使用Python 3.6
Here's the code: 这是代码:
import urllib.request
filelink = 'https://randomwebsite.com/changelog.txt'
filename = filelink.rsplit('/', 1)
filename = str(filename[1])
urllib.request.urlretrieve(filelink, filename)
I want to include a header to give me the permission to download the file but I need to keep a line like the last one, using the two variables (one for the link of the file and one for the name that depends of the link). 我想包含一个标题,以授予我下载文件的权限,但我需要使用两个变量(最后一个保留一行)(一个用于文件的链接,另一个用于依赖链接的名称) 。
Thanks already for your help ! 已经感谢您的帮助!
Check the below link: https://stackoverflow.com/a/7244263/5903276 检查以下链接: https : //stackoverflow.com/a/7244263/5903276
The most correct way to do this would be to use the urllib.request.urlopen function to return a file-like object that represents an HTTP response and copy it to a real file using shutil.copyfileobj. 最正确的方法是使用urllib.request.urlopen函数返回表示HTTP响应的类似文件的对象,然后使用shutil.copyfileobj将其复制到真实文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.