Python third-party library requests Learning

Source: Internet
Author: User
Tags send cookies

1. Installation

1 git clone git://github.com/kennethreitz/requests.git2CD requests3 Python setup.py Install

2. Power on the DOT

(GET)

1>>>ImportRequests2>>> URL ='http://dict.baidu.com/s'3>>> payload = {'WD':'python'}4>>> r = Requests.get (url,params=payload)//The most basic GET request with parameters5<response [200]>6>>>Print(R.url)7Http://dict.baidu.com/s?wd=python8>>>Print(R.text)//decoded data

(POST)

1>>>ImportRequests2>>>ImportJSON3>>> r = Requests.post ('Https://api.github.com/some/endpoint', Data=json.dumps ({'some':'Data'}))4>>>Print(R.json ())5{u'Documentation_url': U'Https://developer.github.com/v3', u'message': U'Not Found'}

Custom Header

1>>>ImportRequests2>>>ImportJSON3>>> data = {'some':'Data'}4>>> headers = {'Content-type':'Application/json','user-agent':'mozilla/5.0 (x84_64;) firefox/22.0'}5>>> r = Requests.post ('Https://api.github.com/some/endpoint', data=data,headers=headers)6>>>Print(R.text)7 {8   "message":"Not Found",9   "Documentation_url":"Https://developer.github.com/v3"Ten}

Timeout settings

 1  requests.get ( " http://github.com  , Timeout=0.001)  2  3 ----------------------4   a bunch of exceptions, focus on the back  5  connecttimeout: Httpconnectionpool (Host= " github.com " Span style= "color: #800000;" > ' , port=80): Max retries exceeded with URL:/(Caused by connecttimeouterror (< Requests.packages.urllib3.connection.HTTPConnection object at 0x01eae6f0>,  connection to github.com timed out. (Connect timeout=0.001)   ) 

accessing cookies

1 Import Requests 2 r = requests.get ('http://www.google.com.hk/')3Print (r.cookies['NID'])4print(tuple ( R.cookies))

You can also send cookies to the server

1 ImportRequests2URL ='http://myserver.org/cookies'3cookies = {'Testcookies_1':'Hello_python3','testcookies_2':'hello_requests'}4 #in Cookie Version 0, special symbols such as spaces, brackets, parentheses, equals, commas, double quotes, slashes, question marks, @, colons, semicolons, etc., are not allowed as the contents of the cookie. 5r = Requests.get (URL, cookies=cookies)6 Print(R.json ())

Python third-party library requests Learning

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.