1, Requests module description
Requests is an HTTP library that uses Apache2 Licensed licenses. Written in Python, really for the sake of mankind.
The urllib2 module in the Python Standard library provides most of the HTTP functionality you need, but its API is too slag-off. It was created for another era, another internet. It requires a huge amount of work, even covering a variety of methods, to accomplish the simplest task.
In Python's world, things shouldn't be so troublesome.
Requests uses URLLIB3, so it inherits all of its features. Requests supports HTTP connection retention and connection pooling, supports the use of cookies to maintain sessions, supports file uploads, supports automatic encoding of response content, supports internationalized URLs and automatic POST data encoding. Modern, international, humane.
(The above is transferred from requests official documents)
2. Requests Module Installation
Then perform the installation
Python setup.py Install
Personal recommendation to use PIP installation
PIP Install requests
You can also use Easy_install to install
Easy_install Requests
Try to import requests in the IDE, if there is no error, then the installation is successful.
3, requests module Simple introduction
#HTTP请求类型 #get Type R= requests.Get('Https://github.com/timeline.json') #post类型r= Requests.post ("Http://m.ctrip.com/post") #put类型r= Requests.put ("Http://m.ctrip.com/put") #delete类型r= Requests.delete ("Http://m.ctrip.com/delete") #head类型r= Requests.head ("Http://m.ctrip.com/head") #options类型r= Requests.options ("Http://m.ctrip.com/get") #获取响应内容print r.content #以字节的方式去显示, Chinese display as character print R.text #以文本的方式去显示 #url pass parameters Payload= {'keyword':'Japan','Salecityid':'2'}r= requests.Get("http://m.ctrip.com/webapp/tourvisa/visa_list",params=payload) Print R.url #示例为http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword= Japan#获取/Modify page encoding R= requests.Get('Https://github.com/timeline.json') Print r.encodingr.encoding='Utf-8'#json处理r= requests.Get('Https://github.com/timeline.json') Print R.json () #需要先import JSON #定制请求头url='http://m.ctrip.com'Headers= {'user-agent':'mozilla/5.0 (Linux; Android 4.2.1; En-us; Nexus 4 build/jop40d) applewebkit/535.19 (khtml, like Gecko) chrome/18.0.1025.166 Mobile safari/535.19'}r= Requests.post (URL, headers=headers) Print r.request.headers# complex POST request URL='http://m.ctrip.com'Payload= {'some':'Data'}r= Requests.post (URL, data=json.dumps (payload)) #如果传递的payload是string而不是dict, you need to first call the dumps method to format the #post multipart encoded file URL='http://m.ctrip.com'Files= {'file': Open ('Report.xls','RB')}r= Requests.post (URL, files=files) #响应状态码r= requests.Get('http://m.ctrip.com') Print R.status_code #响应头r= requests.Get('http://m.ctrip.com') Print R.headersprint r.headers['Content-type']print r.headers.Get('Content-type') #访问响应头部分内容的两种方式 #Cookiesurl='Http://example.com/some/cookie/setting/url'R= requests.Get(URL) r.cookies['Example_cookie_name'] #读取cookies URL='http://m.ctrip.com/cookies'Cookies= Dict (cookies_are='Working') R= requests.Get(URL, cookies=cookies) #发送cookies # set timeout time R= requests.Get('http://m.ctrip.com', timeout=0.001) #设置访问代理proxies= { "http":"http://10.10.10.10:8888", "HTTPS":"http://10.10.10.100:4444",}r= requests.Get('http://m.ctrip.com', proxies=proxies)4. Requests example
JSON request
#!/user/bin/Env python#coding=utf-8Import Requestsimport JSONclassUrl_request (): Def __init__ (self):"""Init""" if__name__=='__main__': Headers= {'Content-type':'Application/json'} payload= {'CountryName':'China', 'Provincename':'Shaanxi Province', 'L1cityname':'Hanzhong', 'L2cityname':'Chenggu', 'Townname':"', 'Longitude':'107.33393', 'Latitude':'33.157131', 'Language':'CN'} R= Requests.post ("http://www.xxxxxx.com/CityLocation/json/LBSLocateCity", Headers=headers,data=payload) #r. Encoding='Utf-8'Data=R.json ()ifr.status_code!= $: Print"lbslocatecity API Error"+str (r.status_code) print data['cityentities'][0]['Cityid'] #打印返回json中的某个key的value print data['ResponseStatus']['Ack'] Print json.dumps (data,indent=4, Sort_keys=true,ensure_ascii=false) #树形打印json, ensure_ascii must be set to False otherwise Chinese will be displayed as Unicode
XML request
#!/user/bin/Env python#coding=utf-8Import RequestsclassUrl_request (): Def __init__ (self):"""Init""" if__name__=='__main__': Headers= {'Content-type':'Text/xml'} XML='<?xml version= "1.0" encoding= "Utf-8"? ><soap:envelope xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "xmlns:soap=" http://schemas.xmlsoap.org/soap/ envelope/"><soap:body><request xmlns=" http://tempuri.org/"><jme><JobClassFullName> wechatjsticket.jobws.job.jobrefreshticket,wechatjsticket.jobws</jobclassfullname><action>run</ Action><param>1</param>'URL='Http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx'R= Requests.post (url,headers=headers,data=XML) #r. Encoding='Utf-8'Data=r.text Print Data5. Reference documents
http://cn.python-requests.org/en/latest/
Http://docs.python-requests.org/en/latest/user/quickstart.html
Python Requests Module Learning notes