@zhengyuhong
2016-03-21T14:03:56.000000Z
字数 3114
阅读 1311
requests python
import requestsr = requests.get('https://github.com/timeline.json') # 返回Response对象payload = {'key1': 'value1', 'key2': 'value2'}r = requests.get('https://github.com/timeline.json', params=payload)
r = requests.put("http://httpbin.org/put")
response = requests.post("http://httpbin.org/post")
import jsonurl = 'https://api.github.com/some/endpoint'payload = {'some': 'data'}headers = {'content-type': 'application/json'}r = requests.post(url, data=json.dumps(payload), headers=headers)
payload = {'key1': 'value1', 'key2': 'value2'}r = requests.post("http://httpbin.org/post", data=payload)#r = requests.post(url, data=json.dumps(payload), headers=headers)print r.text"""{..."form": {"key2": "value2","key1": "value1"},...}"""
读取响应的内容。再次以Github时间线为例:
r = requests.get('https://github.com/timeline.json')print r.text# '[{"repository":{"open_issues":0,"url":"https://github.com/...
#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'http://www.exanple.com/login'#登陆地址url2 = "http://www.example.com/main"#需要登陆才能访问的页面地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;","Accept-Encoding":"gzip","Accept-Language":"zh-CN,zh;q=0.8","Referer":"http://www.example.com/","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"}res1 = s.post(url1, data=data)res2 = s.post(url2)print(resp2.content)
get_adapter(url)Returns the appropriate connnection adapter for the given URL.
post(url, data=None, **kwargs)Sends a POST request. Returns Response object.参数:url – URL for the new Request object.data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.**kwargs – Optional arguments that request takes.
以字节的方式访问请求响应体,对于非文本请求,如获取图片:
r = requests.get('https://github.com/timeline.json')print r.content# b'[{"repository":{"open_issues":0,"url":"https://github.com/...
Requests中也有一个内置的JSON解码器,助你处理JSON数据:
r = requests.get('https://github.com/timeline.json')print r.json()# [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
import requestsr = requests.get('https://github.com/timeline.json') # 返回Response对象print r.urlpayload = {'key1': 'value1', 'key2': 'value2'}r = requests.get('https://github.com/timeline.json', params=payload)print r.url
检测响应状态码
r = requests.get('http://httpbin.org/get')r.status_code# 200
为方便引用,Requests还附带了一个内置的状态码查询对象:
r.status_code == requests.codes.okTrue
查看以一个Python字典形式展示的服务器响应头:
r = requests.get('http://httpbin.org/get')print r.headers
但是这个字典比较特殊:它是仅为HTTP头部而生的。根据 RFC 2616 , HTTP头部是大小写不敏感的。
因此,我们可以使用任意大写形式来访问这些响应头字段:
r.headers['Content-Type']# 'application/json; charset=utf-8'r.headers.get('content-type')# 'application/json; charset=utf-8'
检测收到的cookies
url = 'http://example.com/some/cookie/setting/url'r = requests.get(url)r.cookies['example_cookie_name']# 'example_cookie_value'
发送定制cookies
url = 'http://httpbin.org/cookies'cookies = dict(cookies_are='working')r = requests.get(url, cookies=cookies)print r.text# '{"cookies": {"cookies_are": "working"}}'
