Python Requests Module Learning

Source: Internet
Author: User

Import requests

You can use the Magic requests module below!

1. Sending data to a Web page

>>> payload = {'Key1':'value1','Key2': ['value2','Value3']}>>> r = Requests.get ('Http://httpbin.org/get', params=Payload)>>>Print(r.url) http:Httpbin.org/get?key1=value1&key2=value2&key2=value3

The post is similar to the get method, except that it is not params= ... But data= ...

>>> r = requests.put ("http://httpbin.org/put")>>> r = Requests.delete ("http://httpbin.org/delete")>>> r = Requests.head ( " Http://httpbin.org/get " )>>> r = requests.options ("http://httpbin.org/get")

2. Save the response to a file (official document recommended METHOD)

' WB ' as FD    :for in  r.iter_content (chunk_size):        fd.write (chunk)

3. Some properties of R

R.encoding #编码

R.status_code #状态码 (404,200 Etc.)

R.raise_for_status () #如果状态码不是200会发起报错

4. Advanced Usage (session object-emulation Web page operation): http://cn.python-requests.org/zh_CN/latest/user/advanced.html#advanced

  

Python Requests Module 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.