Python 网络请求模块 urllib 、requests
Python 给人的印象是抓取网页非常方便,提供这种生产力的,主要依靠的就是 urllib、requests这两个模块。
urlib 介绍
urllib.request 提供了一个 urlopen 函数,来实现获取页面。支持不同的协议、基本验证、cookie、代理等特性。
urllib 有两个版本 urllib 以及 urllib2。
urllib2 能够接受 Request 对象,urllib 则只能接受 url。
urllib 提供了 urlencode 函数来对GET请求的参数进行转码,urllib2 没有对应函数。
urllib 抛出了 一个 URLError 和一个 HTTPError 来处理客户端和服务端的异常情况。
Requests 介绍
Requests 是一个简单易用的,用Python编写的HTTP库。这个库让我们能够用简单的参数就完成HTTP请求,而不必像 urllib 一样自己指定参数。同时能够自动将响应转码为Unicode,而且具有丰富的错误处理功能。
- International Domains and URLs
- Keep-Alive & Connection Pooling
- Sessions with Cookie Persistence
- Browser-style SSL Verification
- Basic/Digest Authentication
- Elegant Key/Value Cookies
- Automatic Decompression
- Unicode Response Bodies
- Multipart File Uploads
- Connection Timeouts
- .netrc support
- List item
- Python 2.6—3.4
- Thread-safe
以下为一些示例代码,本文环境为 Python 3.6.0
无需参数直接请求单个页面
import urllib
from urllib.request import request
from urllib.urlopen import urlopen
# import urllib2
import requests
# 使用 urllib 方式获取
response = urllib.request.urlopen('http://www.baidu.com')
# read() 读取的是服务器的原始返回数据 decode() 后会进行转码
print(response.read().decode())
# 使用 requests 方式获取
# request 模块相比
resp = requests.get('http://www.baidu.com')
print(resp)
print(resp.text)
HTTP 是基于请求和响应的工作模式,urllib.request 提供了一个 Request 对象来代表请求,因此上面的代码也可以这么写
req = urllib.request.Request('http://www.baidu.com')
with urllib.request.urlopen(req) as response:
print(response.read())
Request对象可以增加header信息
req = urllib.request.Request('http://www.baidu.com')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
with urllib.request.urlopen(req) as response:
print(response.read())
或者直接将 header 传入 Request 构建函数。
带参数的 GET 请求
带有参数的请求和上面的例子本质一样,可以事先拼出URL请求字符串,然后再进行请求。
本例使用了 腾讯 的股票API,可以传入不同的股票代码以及日期,查询对应股票在对应时间的价格、交易信息。
# 使用带参数的接口访问
tencent_api = "http://qt.gtimg.cn/q=sh601939"
response = urllib.request.urlopen(tencent_api)
# read() 读取的是服务器的原始返回数据 decode() 后会进行转码
print(response.read())
resp = requests.get(tencent_api)
print(resp)
print(resp.text)
发送 POST 请求
urllib 没有单独区分 GET 和 POST 请求的函数,只是通过 Request 对象是否有 data 参数传入来判断。
import urllib.parse
import urllib.request
url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
data = urllib.parse.urlencode(values)
data = data.encode('ascii') # data should be bytes req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
the_page = response.read()
参考资料:
1、python3 urllib.request 网络请求操作
2、Python3学习笔记(urllib模块的使用)
3、Python模拟登录的几种方法
4、What are the differences between the urllib, urllib2, and requests module?
5、python3 urllib和requests模块
Python 网络请求模块 urllib 、requests的更多相关文章
- Python网络请求urllib和urllib3详解
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...
- 【python网络爬虫】之requests相关模块
python网络爬虫的学习第一步 [python网络爬虫]之0 爬虫与反扒 [python网络爬虫]之一 简单介绍 [python网络爬虫]之二 python uillib库 [python网络爬虫] ...
- python 网络请求类库 requests 使用
python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...
- python网络爬虫之二requests模块
requests http请求库 requests是基于python内置的urllib3来编写的,它比urllib更加方便,特别是在添加headers, post请求,以及cookies的设置上,处理 ...
- aiohttp 支持异步的网络请求模块
通常在进行网络数据采集时候我们会用到requests,urllib等模块,但是这些模块在使用中并不支持异步,所以今天我们介绍一个支持异步网络请求的模块aiohttp. 首先我们使用flask简单的搭一 ...
- iOS开发简记(7):网络请求模块
主流的APP都少不了跟服务器交互,网络请求是少不了的事情. 开源的网络请求库,有很多,比如:AFNetworking.YTKNetwork.PPNetworkHelper.ASIHttpRequest ...
- vue(24)网络请求模块axios使用
什么是axios Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 主要的作用:axios主要是用于向后台发起请求的,还有在请求中做更多是可控功能. a ...
- python网络请求简洁之道--python requests简介
#requests中文文档:http://cn.python-requests.org/en/latest/#学习出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0 ...
- 设置python爬虫IP代理(urllib/requests模块)
urllib模块设置代理 如果我们频繁用一个IP去爬取同一个网站的内容,很可能会被网站封杀IP.其中一种比较常见的方式就是设置代理IP from urllib import request proxy ...
随机推荐
- 微信隐藏的webJS Api汇总
1.右侧菜单增加"查看公众账号" API document.getElementById('post-user').addEventListener('click', functi ...
- Linux常用包的安装
下面列出的这些包都有很强大的功能,也是非常实用常用的工具 gcc yum -y install gcc GNU编译器套件(GNU Compiler Collection) gcc-c++ yum -y ...
- java学习之租车系统
背景:有三种类型的车供给用户来租用 要求:控制台用户交互界面,根据用户需求输出租车价格,结果如下: 创建租车类主要设计过程: 创建租车类 创建Car父类,包含四种属性成员,重写构造方法 创建三种 ...
- Spring-Session实现Session共享实现原理以及源码解析
知其然,还要知其所以然 ! 本篇介绍Spring-Session的整个实现的原理.以及对核心的源码进行简单的介绍! 实现原理介绍 实现原理这里简单说明描述: 就是当Web服务器接收到http请求后,当 ...
- 【原创】MySQL Can't create a new thread报错分析
今天有两台服务器都出现了Can't create a new thread报错. [故障处理过程] 故障发生后登录服务器,检查mysql进程正常,但登录mysql报下面错误 ERROR 1135 (H ...
- Redis介绍及部署在CentOS7上(一)
0.Redis目录结构 1)Redis介绍及部署在CentOS7上(一) 2)Redis指令与数据结构(二) 3)Redis客户端连接以及持久化数据(三) 4)Redis高可用之主从复制实践(四) 5 ...
- 开发人员如何正确对待BUG?
1.前端开发与后端开发 出了问题,最重要的是先找到方法迅速解决,而不是去互相指责.前端存在这样的思维模式,后端也存在这样的思维模式,这种思维模式不太好.出了问题,最好先检查一下自己,反省是不是自己这 ...
- hadoop 视频教程3之实战教程
一,视频内容: 海量数据处理平台框架 hadoop介绍 hadoop 生态系统介绍 hdfs 设计原则 hdfs 系统架构 namenode datanode secondarynamenode hd ...
- Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥
E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...
- c# dapper mysql like 参数化
//拼接sql语句: if (!string.IsNullOrEmpty(model.Email)) { where += " and a.email like @email "; ...