requests的介绍和学习
来源:互联网 发布:access数据库好用吗 编辑:程序博客网 时间:2024/05/29 15:48
requests库
作者:1world0x00(说明:在入选本教程的时候,我进行了适当从新编辑)
requests是一个用于在程序中进行http协议下的get和post请求的库。
安装
easy_install requests
或者用
pip install requests
安装好之后,在交互模式下运行:
>>> import requests>>> dir(requests)['ConnectionError', 'HTTPError', 'NullHandler', 'PreparedRequest', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'adapters', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'utils']
从上面的列表中可以看出,在http中常用到的get,cookies,post等都赫然在目。
get请求
>>> r = requests.get("http://www.itdiffer.com")
得到一个请求的实例,然后:
>>> r.cookies<<class 'requests.cookies.RequestsCookieJar'>[]>
这个网站对客户端没有写任何cookies内容。换一个看看:
>>> r = requests.get("http://www.1world0x00.com")>>> r.cookies<<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='PHPSESSID', value='buqj70k7f9rrg51emsvatveda2', port=None, port_specified=False, domain='www.1world0x00.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>
原来这样呀。继续,还有别的属性可以看看。
>>> r.headers{'x-powered-by': 'PHP/5.3.3', 'transfer-encoding': 'chunked', 'set-cookie': 'PHPSESSID=buqj70k7f9rrg51emsvatveda2; path=/', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'keep-alive': 'timeout=15, max=500', 'server': 'Apache/2.2.15 (CentOS)', 'connection': 'Keep-Alive', 'pragma': 'no-cache', 'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'date': 'Mon, 10 Nov 2014 01:39:03 GMT', 'content-type': 'text/html; charset=UTF-8', 'x-pingback': 'http://www.1world0x00.com/index.php/action/xmlrpc'}>>> r.encoding'UTF-8'>>> r.status_code200
下面这个比较长,是网页的内容,仅仅截取显示部分:
>>> print r.text<!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>1world0x00sec</title> <link rel="stylesheet" href="http://www.1world0x00.com/usr/themes/default/style.min.css"> <link rel="canonical" href="http://www.1world0x00.com/" /> <link rel="stylesheet" type="text/css" href="http://www.1world0x00.com/usr/plugins/CodeBox/css/codebox.css" /> <meta name="description" content="爱生活,爱拉芳。不装逼还能做朋友。" /> <meta name="keywords" content="php" /> <link rel="pingback" href="http://www.1world0x00.com/index.php/action/xmlrpc" /> ......
请求发出后,requests会基于http头部对相应的编码做出有根据的推测,当你访问r.text之时,requests会使用其推测的文本编码。你可以找出requests使用了什么编码,并且能够使用r.coding属性来改变它。
>>> r.content'\xef\xbb\xbf\xef\xbb\xbf<!DOCTYPE html>\n<html lang="zh-CN">\n <head>\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>1world0x00sec</title>\n <link rel="stylesheet" href="http://www.1world0x00.com/usr/themes/default/style.min.css">\n <link ......以二进制的方式打开服务器并返回数据。
post请求
requests发送post请求,通常你会想要发送一些编码为表单的数据——非常像一个html表单。要实现这个,只需要简单地传递一个字典给data参数。你的数据字典在发出请求时会自动编码为表单形式。
>>> import requests>>> payload = {"key1":"value1","key2":"value2"}>>> r = requests.post("http://httpbin.org/post")>>> r1 = requests.post("http://httpbin.org/post", data=payload)
r没有加data的请求,看看效果:
r1是加了data的请求,看效果:
多了form项。喵。
http头部
>>> r.headers['content-type']'application/json'
注意,在引号里面的内容,不区分大小写'CONTENT-TYPE'
也可以。
还能够自定义头部:
>>> r.headers['content-type'] = 'adad'>>> r.headers['content-type']'adad'
注意,当定制头部的时候,如果需要定制的项目有很多,需要用到数据类型为字典
0 0
- requests的介绍和学习
- 学习urblib和requests
- python requests 详细学习content 和text 的区别
- Python requests模块的学习
- requests学习
- Requests 学习
- 爬虫学习01 requests入门 response的常用方法 response.text 和response.content的区别
- python学习(6):python爬虫之requests和BeautifulSoup的使用
- python requests模块的安装和使用方法
- requests和bs4的python爬虫入门
- python3 requests的content和text方法
- urllib2和requests的基本使用
- 爬虫 requests库的cookie 和session
- Requests介绍与安装
- python requests库介绍
- Requests之总体介绍
- Python 最好用的第三方 http 库-Requests介绍
- python爬虫之urllib模块和requests模块学习
- UI控件--RecyclerView(1)
- 算法与数据机构学习_第一章.栈和队列_1.设计一个有返回栈中最小元素功能的栈
- python资料全集
- 反射四步走
- JAVA 反射总结
- requests的介绍和学习
- 电影节(SDUT_2797)
- Clang Address Sanitizer
- SPOJ 7258 SUBLEX 后缀自动机
- 如何更方便快捷的调用restful服务
- 利用JS做到隐藏div和显示div
- 新增数据文件导致dataguard同步异常
- java反射以获取父类属性的值
- docker琐碎