Python Requests Module Learning notes

Source: Internet
Author: User
Tags connection pooling send cookies

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)

1. Installation

Click here to download

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.

2. Easy to get started
#HTTP request Type#Get TypeR = Requests.get ('Https://github.com/timeline.json')#Post TypeR = Requests.post ("Http://m.ctrip.com/post")#put typeR = Requests.put ("Http://m.ctrip.com/put")#Delete TypeR = Requests.delete ("Http://m.ctrip.com/delete")#Head TypeR = Requests.head ("Http://m.ctrip.com/head")#Options TypesR = Requests.options ("Http://m.ctrip.com/get")#Get Response ContentPrintR.content#display in bytes, Chinese as charactersPrintR.text#To display in a text way#URL Pass ParametersPayload = {'keyword':'Japan','Salecityid':'2'}r= Requests.get ("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload)PrintR.url#Example for http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword= Japan#Get/Modify page encodingR = Requests.get ('Https://github.com/timeline.json')Printr.encodingr.encoding='Utf-8'#JSON processingR = Requests.get ('Https://github.com/timeline.json')PrintR.json ()#need to import JSON first#Custom Request HeadersURL ='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)Printr.request.headers#Complex POST RequestsURL ='http://m.ctrip.com'Payload= {'some':'Data'}r= Requests.post (URL, data=json.dumps (payload))#if the passed payload is a string instead of a dict, you need to call the dumps method first to format the#Post Multi-part encoding fileURL ='http://m.ctrip.com'Files= {'file': Open ('Report.xls','RB')}r= Requests.post (URL, files=files)#Response Status CodeR = Requests.get ('http://m.ctrip.com')PrintR.status_code#Response HeaderR = Requests.get ('http://m.ctrip.com')Printr.headersPrintr.headers['Content-type']PrintR.headers.get ('Content-type')#two ways to access the content of the response header    #CookiesURL ='Http://example.com/some/cookie/setting/url'R=requests.get (URL) r.cookies['Example_cookie_name']#Read CookiesURL='http://m.ctrip.com/cookies'Cookies= Dict (cookies_are='Working') R= Requests.get (URL, cookies=cookies)#Send Cookies#setting the time-out periodR = Requests.get ('http://m.ctrip.com', timeout=0.001)#setting up an access agentproxies = {           "http":"http://10.10.10.10:8888",           "HTTPS":"http://10.10.10.100:4444",}r= Requests.get ('http://m.ctrip.com', proxies=proxies)
3. Example
#!/user/bin/env python#Coding=utf-8ImportRequestsImportJSONclassurl_request ():def __init__(self):"""Init"""    if __name__=='__main__': 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'} 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 ()PrintR.textifr.status_code!=200:        Print "lbslocatecity API Error"+Str (r.status_code)Printdata['cityentities'][0]['Cityid']#Print returns the value of a key in JSON    Printdata['ResponseStatus']['Ack']

Reference Documentation:

http://cn.python-requests.org/en/latest/

Http://docs.python-requests.org/en/latest/user/quickstart.html

Python Requests Module Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.