简体   繁体   English

使用urllib2 vs wget在python中下载文件

[英]Download file in python using urllib2 vs wget

I am trying to download an online file in python. 我正在尝试使用python下载在线文件。 I have seen solutions using urllib2 for python 2 and wget. 我已经看到了针对python 2和wget使用urllib2的解决方案。 If my purpose is just to download the file, is there any advantage of using urllib2 instead of wget. 如果我的目的只是下载文件,那么使用urllib2而不是wget有什么好处。 To me using wget package seems simpler. 对我来说,使用wget包似乎更简单。 However, most of the online solutions I see are using urllib2 and urllib with python 3. I am more inclined towards wget as it works for both python 2 and python 3. 但是,我看到的大多数在线解决方案都将urllib2和urllib与python 3一起使用。我更倾向于wget,因为它同时适用于python 2和python 3。

This question is different from the question marked as duplicate of this as I have asked for difference with respect to wget, while the other question does not address the relationship with respect to wget package. 这个问题与标记为重复的问题有所不同,因为我要求对wget进行区别,而另一个问题并未解决与wget软件包有关的关系。

If you use wget then you'd end up writing way much more code when needing to decode the errors that happen with it, than you'd need for a Python library. 如果使用wget那么当需要解码它所发生的错误时,与使用Python库相比,最终将编写更多的代码。

However, when it comes to urllib - first of all, are you sure you really need to support Python 2? 但是,当涉及到urllib -首先,您确定确实需要支持Python 2吗? Python 2 is obsolete. Python 2已过时。

If you really really believe that you do, then perhaps you can use a compatibility library such as six or the future 如果您真的相信自己这样做,那么也许可以使用兼容库,例如sixfuture

And you should consider alternatives too - the requests library is superior to the urllib and provides the same interface for Python 2 and 3. 而且您还应该考虑其他选择- 请求库优于urllib并为Python 2和3提供相同的接口。

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

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