Python第三方包 requests还是urllib?

我注意到一个很奇怪的事情。在极客学院爬虫课视频里,老师说要用requests,但是在其他学习群里,很多同学都在讨论urllib/urllib2相关。

然后就迷惑了,为什么会有这三个东西呢?扒了扒国内的博客网站,大多都是urllib相关,并且不推荐使用requests包。于是去墙外搜了下,竟然发现国外大多推荐用requests。我完全愣到了,完全相反的意见。

再仔细八一下,发现国内很多博客博主,只是说不建议使用requests,然后就介绍urllib,并没有说为什么不选requests;国外的稍微好些,有很多相关的讨论。

因为我本来就是刚接触的就是requests,肯定是喜欢它多一点,于是看到一篇做比较的博文,就转过来了(如下文)。

另,在stackoverflow看到一个相关问题以及讨论,这个能访问,就不转了。

我就是觉得requests好哈哈哈哈



urllib/urllib2 vs requests package in Python

转自:http://avi-urllib-vs-requests.blogspot.com/

Python contains libraries to interact with websites or used for opening HTTP URLs.

Example:urllib/urllib2 , requests.

1.urllib/urllib2:

· Urllib is a python module used for opening HTTP URLs.

· It accomplish tasks such as basic authentication, getting cookies, serving GET/POST requests, error handling, viewing headers.

· Urllib2 is an improved Python module and provides additional functionalities to several methods.

· Hence some urllib methods have been replaced by urllib2 methods.

· In spite of having additional features, urllib cannot be completely replaced by urllib2 since the former provides important methods (e.g., urlencode(), used for generating GET query strings) that are absent in urllib2.

2.Python Requests:

· ‘Requests’ is a simple, easy-to-use HTTP library written in Python.

· Requests makes interacting with Web services seamless.

Features of Python Requests:

· Connection pooling: There is a pool of connections, and a connection is released only once all its data has been read.

· Sessions with cookie persistence: You can make a session object and set certain parameters and cookie values. This allows you to persist these parameters and cookies across all requests made from the session instance.

· 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.

· Python Requests automatically decodes the response into Unicode.

· Python Requests handles multi-part file uploads, as well as automatic form-encoding.

· In Python, Requests .get() is a method, auth is an optional parameter (since we may or may not require authentication).

· Python Requests supports the entire restful API, i.e., all its methods – PUT, GET, DELETE, POST.

· Unlike the urllib/urllib2 module, there is no confusion caused by Requests, as there is only a single module that can do the entire task.

· Can write easier and shorter code.

Comparison between Python Requests and urllib/urllib2:

Example 1: A simple HTTP GET request and authentication

Using urllib2: In this example, to make a simple HTTP GET request we need to call a lot of methods.

Remembering the names of these methods can be difficult:

import urllib2

url = ‘https://www.example.com

username= ‘user’

password = ‘pass’

request = urllib2.Request(url)

password_manager = >urllib2.HTTPPasswordMgrWithDefaultRealm()

password_manager.add_password(None, url, username, password)

auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)

opener = urllib2.build_opener(auth_manager)

urllib2.install_opener(opener)

handler = urllib2.urlopen(request)

print handler.getcode()

print handler.headers.getheader(‘content-type’)

Using Requests: The task of making a simple HTTP GET request can be accomplished in a single line when compared to the large code written using urllib2.

import requests

r = requests.get(‘https://www.example.com‘, auth=(‘user’, ‘pass’))

print r.status_code

print r.headers[‘content-type’]

Example 2: Making a POST request

Using urllib2/urllib: Note that in this example we had to make use of both the urllib and urllib2 modules in order to write a script for a simple POST request:

import urllib

import urllib2

url = “http://www.example.com

values = {“firstname”:” abc “, “lastname”:” xyz “}

header = {“User-Agent”:”Mozilla/4.0 (compatible; MSIE 5.5;Windows NT)”}

values = urllib.urlencode(values)

request = urllib2.Request(url, values, header)

response = urllib2.urlopen(request)

html_content = response.read()

Using Requests: Here we do not require import multiple modules and a single requests module can accomplish the entire task:

import requests

values = {“”firstname”:” abc “, “lastname”:” xyz “}

r = requests.post(‘https://www.example.com, data=values)

I hope from above examples it is clear that requests library is very much easy to use .

Thanks guys :) for reading this ,please share your comments or thoughts.

时间: 2024-08-06 03:42:35

Python第三方包 requests还是urllib?的相关文章

python第三方库requests简单介绍

一.发送请求与传递参数 简单demo: import requests r = requests.get(url='http://www.itwhy.org') # 最基本的GET请求 print(r.status_code) # 获取返回状态 r = requests.get(url='http://dict.baidu.com/s', params={'wd':'python'}) #带参数的GET请求 print(r.url) print(r.text) #打印解码后的返回数据 1.带参数

Python第三方包的安装

不得不说我又变懒了,过个年都懒得学习了 之前不太明白包的管理,都是用一个搜一下 今天总结一下 python第三方包安装的两种方式 下载源码然后setup.py安装 用pip进行安装 第一种方法 在网上找到需要的包,下载解压,找到setup.py cmd切换到包的目录,输入python setup.py install 第二种方法 cmd切换到Python27\Scripts目录 敲入pip install xxx(包名)

Python 第三方包上传至 PyPI 服务器

PyPI 服务器主要功能是?PyPI 服务器怎么搭建? PyPI 服务器可以用来管理自己开发的 Python 第三包. Pypi服务器搭建 Python 第三方包在本地打包 # 本地目录执行以下命令应该能成功在dist目录下生成*.tar.gz的包文件. python setup.py sdist Python 第三方包上传至 PyPI服务器 代码打包服务器上家目录添加配置文件 vim ~/.pypirc 添加如下内容: [distutils] index-servers = test test

Python第三方包之PrettyTable

Python第三方包之PrettyTable 可以让我们将数据用表格的方式展示出来 安装方式 pip install PrettyTable 测试是否安装成功 使用方法与对比 增加一条数据 先简单的看下如何使用以及效果 import prettytable table = prettytable.PrettyTable() # 定义表头 table.field_names = ['name','age','sex'] # 增加一行数据,列表里的元素按照顺序对应表头 table.add_row([

python第三方库requests详解

Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求.Requests 的哲学是以 PEP 20 的习语为中心开发的,所以它比 urllib 更加 Pythoner.更重要的一点是它支持 Python3 哦! Beautiful is better than ugly.(美丽优于丑陋) Explicit is better than im

安装python第三方包

20190822 先说一下,有4种方法(我知道的): 1.源码安装 2.python自带包管理器安装 3.外部包管理器安装 4.whl格式安装 1.源码安装 Python第三方库几乎都可以在github或者 pypi上找到源码.源码包格式大概有zip . tar.zip. tar.bz2.解压这些包,进入解压好的文件夹,通常会有一个setup.py的文件.打开命令行,进入该文件夹.运行以下命令,就能把这个第三库安装到系统里: python setup.py install 或者借助pip,则不需

Python第三方库Requests学习

1.安装 1 git clone git://github.com/kennethreitz/requests.git 2 cd requests 3 python setup.py install 2.上点威力 (GET) 1 >>> import requests 2 >>> url = 'http://dict.baidu.com/s' 3 >>> payload = {'wd':'python'} 4 >>> r = requ

Python第三方包的安装和管理

要想安装第三方的包,首先安装setuptools. 然后再安装pip,这个是用来管理第三方包的.(ActivePython里面应该预装了这些.) 1. 包管理器pip,安装第三方包,以Selenium为例,运行 pip install selenium 卸载包 pip uninstall selenium 2. 查看已经安装的第三方包 新版pip:pip list 旧版pip:pip freeze 3. 升级包foo pip install -upgrade foo 列出需要升级的包 pip l

安装python第三方包的几种方式

除了内置的标准库,python还支持通过自动开发的库或众多丰富的第三方库进行扩展. Python的第三方支持工具包括网站开发.数值计算.串口读写.游戏开发等各个方面. 我平时使用比较多的第三方库的下载地址是   https://pypi.org/   ,基本上只要是你能想到的在上面都可以找到. 下面总结几种常见的导入方式: 1. 直接通过CMD窗口,使用pip命令进行自动下载安装 此方法使用的前提是已经安装了pip这个方法,不过在新的python安装包中已经继承了pip方法,可以直接使用.(如果