[Python learning] use of the requests library

Source: Internet
Author: User

1. Get request

#-*-coding:utf-8-*-Importrequestsurl_ip="http://b.com/index.php"Pyload= {'Cate': 1,'ID': 2}headers= {'user-agent':'mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/60.0.3112.101 safari/537.36'}defuse_simple_requests (): R= Requests.get (Url_ip, Params=pyload, headers=headers)PrintR.url#resuest URL    PrintR.headers#Response Header    PrintR.text#response content (after encoding)    PrintR.content#Response Content (no encoding)    PrintR.status_code#HTTP status Code    PrintR.json ()#output JSON data, the server returned is not JSON content will be error    #print R.raw.read () #获取原始套接字相应, the request needs to be set Stream=trueuse_simple_requests ()

2. Post request

#-*-coding:utf-8-*-Importrequestsurl_ip="http://b.com/index.php"Pyload= {'Cate': 1,'ID': 2}defuse_simple_requests (): R= Requests.post ('Http://httpbin.org/post', Data=pyload)Printr.textuse_simple_requests ()

3. Cookies

Server side: http://a.com/index.php

<? PHP     $cookie _name $_cookie [' name '];       Accept the cookie    sent by the clientsetcookie(' name ',$cookie _name. 777 ');  Send the accepted cookie value to the client after appending ' 777 ' ?>

Client: request_demo.py

#-*-coding:utf-8-*-Importrequestsurl_ip="http://b.com/index.php"Cookies= {'name':'testtest'}#Set Cookiesdefuse_simple_requests (): R= Requests.get (url_ip, cookies=cookies)#bring a cookie on request    Printr.headersPrintr.cookies['name']#Print Cookiesuse_simple_requests ()

Documentation: http://docs.python-requests.org/en/master/user/quickstart/#make-a-request

[Python learning] use of the requests library

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.