Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which urllib I should choose? [duplicate]

as we know, python has two built-in url lib:

  • urllib
  • urllib2

and a third-party lib:

  • urllib3

if my requirement is only to request a API by GET method, assume it return a JSON string.
which lib I should use? do they have some duplicated functions?
if the urllib can implement my require, but after if my requirements get more and more complicated, the urllib can not fit my function, I should import another lib at that time, but I really want to import only one lib, because I think import all of them can make me confused, I think the method between them are totally different.

so now I am confused which lib I should use, I prefer urllib3, I think it can fit my requirement all time, how do you think?

like image 629
Mathew P. Jones Avatar asked Dec 09 '13 09:12

Mathew P. Jones


People also ask

Which is better Urllib or requests?

Requests - Requests' is a simple, easy-to-use HTTP library written in Python. 1) Python Requests encodes the parameters automatically so you just pass them as simple arguments, unlike in the case of urllib, where you need to use the method urllib. encode() to encode the parameters before passing them.

Is Urllib faster than requests?

I found that time took to send the data from the client to the server took same time for both modules (urllib, requests) but the time it took to return data from the server to the client is more then twice faster in urllib compare to request. I'm working on localhost.

What does Urllib request Urlretrieve do?

What does Urllib request Urlretrieve do? In line 14, the urllib. request. urlretrieve() function is used to retrieve the image from the given url and store it to the required file directory.

Is Urllib included in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


3 Answers

As Alexander says in the comments, use requests. That's all you need.

like image 150
Daniel Roseman Avatar answered Sep 17 '22 14:09

Daniel Roseman


I don't really know what you want to do, but you should try with requests. It's simple and intuitive.

like image 37
Zulu Avatar answered Sep 17 '22 14:09

Zulu


Personally I avoid to use third-party library when possible, so I can reduce the dependencies' list and improve portability. urllib and urllib2 are not mutually exclusive and are often mixed in the same project.

like image 39
smeso Avatar answered Sep 20 '22 14:09

smeso