What are the differences between the urllib, urllib2, urllib3 and requests module?

后端 未结 11 2291
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 04:19

In Python, what are the differences between the urllib, urllib2, urllib3 and requests modules? Why are there three? They seem to do the same thing...

11条回答
  •  长情又很酷
    2020-11-22 05:04

    urllib and urllib2 are both Python modules that do URL request related stuff but offer different functionalities.

    1) urllib2 can accept a Request object to set the headers for a URL request, urllib accepts only a URL.

    2) urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2.

    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.

    2) It automatically decoded the response into Unicode.

    3) Requests also has far more convenient error handling.If your authentication failed, urllib2 would raise a urllib2.URLError, while Requests would return a normal response object, as expected. All you have to see if the request was successful by boolean response.ok

提交回复
热议问题