Python urllib, urllib2, urllib3 and requests (with some individual opinion) the difference between

Likianta Me 2022-09-09 01:23:48 阅读数:984

pythonurlliburllib2urlliburllib3

urllib

python standard http library, both python 2 and python 3.

urllib2

urllib2 is an http standard library existing only in python 2.

In the era of python 2, because the functions of urllib were too basic and rudimentary, the official did a lot of redesign and launched urllib2.

It is worth noting that urllib2 cannot be used completely independently, and some basic functions still need to be imported from urllib, so you may see "urllib" and "urllib2" coexist in the code written in python 2"Wonderful Scenery":

# python 2 code demofrom urllib import urlencodefrom urllib2 import Request, urlopenreq = Request(url='http://www.example.com')req.add_header('User-Agent', 'example fetcher')req.add_data(urlencode({'foo', 'bar'}))resp = urlopen(req)...

(Personally, it is not friendly to use the python standard library for network communication in the python 2 era.)

Fortunately, python 3 has removed urllib2, leaving only the urllib standard library, and urllib has been greatly improved in function and ease of use, including many commonly used functions (although in terms of humanization andThere is a gap in requests).

urllib3

A third-party library, not included with python. It can be installed by pip install urllib3.

urllib3 This library should have nothing to do with urllib and urllib2. At most, it can be considered that the author of the library used the version naming order of the 2 era in order to reduce people's cognitive burden. (But for beginners who do not understand this history,Saying it's confusing enough.)

urllib3 supports python 2.7+ and python 3.6+. It is used by many well-known libraries, such as requets and pip which depend on it.

ps: My earliest contact with crawlers started with requests, and I didn't know much about urllib3; plus the 1.26+ version of urllib3 caused a disastrous pip proxy exception on Windows (refer to Thisarticle), I have a poor impression of it.

requests

A very popular http library. It has a typical code style, a very easy-to-use (human-friendly) interface design and many other advantages. It is one of the developers' favorite python third-party libraries.

ps: In my impression, requests have more dependencies (but the latest version seems to be very few), and people who are very concerned about the package size still need to pay more attention.

Summary

If it is some basic and occasionally used (in a few modules of your own project), it is actually recommended to use the urllib that comes with python; requests are suitable for those who focus on network communication projects such as crawlers.

Reference

https://stackoverflow.com/a/63233379

版权声明:本文为[Likianta Me]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/252/202209090109477537.html