带有自定义标头的 urllib.urlretrieve [英] urllib.urlretrieve with custom header

查看:17
本文介绍了带有自定义标头的 urllib.urlretrieve的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 urlretrieve 检索文件,同时添加自定义标头.

I am trying to retrieve a file using urlretrieve, while adding a custom header.

在检查 urllib.request 的代码源时,我意识到 urlopen 可以在参数中接受一个 Request 对象,而不仅仅是一个字符串,允许放我想要的标题.但是如果我尝试对 urlretrieve 做同样的事情,我会得到一个 TypeError: expected string or正如在另一篇文章中提到的那样,类似字节的对象.

While checking the codesource of urllib.request I realized urlopen can take a Request object in parameter instead of just a string, allowing to put the header I want. But if I try to do the same with urlretrieve, I get a TypeError: expected string or bytes-like object as mentionned in this other post.

我最终做的是重写我自己的 urlretrieve,删除抛出错误的行(该行与我的用例无关).

What I ended up doing is rewriting my own urlretrieve, removing the line throwing the error (that line is irrelevant in my use case).

它工作正常,但我想知道是否有一种更好/更清洁的方法,而不是重写我自己的urlretrieve.如果可以将自定义标头传递给 urlopen,感觉应该可以用 urlretrieve 做同样的事情?

It works fine but I am wondering if there is a better/cleaner way of doing it, rather than rewriting my own urlretrieve. If it is possible to pass a custom header to urlopen, it feels like it should be possible to do the same with urlretrieve?

推荐答案

我找到了一种方法,您只需添加几行额外的代码...

I found a way where you only have to add a few extra lines of code...

import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve("type URL here", "path/file_name")

如果您想了解详细信息,可以参考 python 文档:https://docs.python.org/3/library/urllib.request.html

Should you wish to learn about the details you can refer to the python documentation: https://docs.python.org/3/library/urllib.request.html

这篇关于带有自定义标头的 urllib.urlretrieve的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆