Getting Started with Python requests basics

Source: Internet
Author: User
First, the Urllib2 module in the Python Standard library provides most of the HTTP functionality you need, but its API is unfriendly. It was created for another era, another internet. It needs a huge amount of work, even covering a variety of methods to complete the simplest task, so learn reuqests module, relatively simple and easy to use (later will learn scapy, more powerful library), installation is needless to say

1 Importing Modules

>>> Import Requests

2 Intuitively feel the simplicity of sending a request

>>> r = requests.get (' Your URL ')
>>> r = requests.post (' Your URL ')
#put The Delete Head options method is also used

3 Passing parameters to URLs

>>> url_params = {' key ': ' Value '} #字典传递参数 if the key with a value of none is not added to the URL
>>> r = requests.get (' Your url ', params = url_params)
>>> Print (R.url)
Your Url?key=value

4 content of the response

>>> r.encoding #获取当前的编码
>>> r.encoding = ' Utf-8 ' #设置编码
>>> R.text #以encoding解析返回内容
>>> r.content #以字节形式 (binary) return
>>> R.json () #以json形式返回, if the content returned is guaranteed to be in JSON format, otherwise parsing error will throw an exception

5 Custom headers and Coookie information

>>> Header = {' user-agent ': ' mozilla/5.0 (Windows NT 10.0) applewebkit/537.36 (khtml, like Gecko) chrome/38.0.212 5.122 safari/537.36 SE 2.X METASR 1.0 '}
>>> cookie = {' key ': ' Value '}
>>> r = requests.get/post (' Your URL ', Headers=header,cookies=cookie)

6 sending a POST data request

>>> send = {' key ': ' Value '}
>>> r = requests.post (' Your URL ', data = send) # If you pass a string instead of a dict, then the data will be published directly, this can also upload files

7 Response Status Code

>>> R.status_code #如果不是200, you can throw an exception using R.raise_for_status ()

8 response

>>> r.headers #返回字典类型, head information
# R.requests.headers Returns the header information sent to the server
>>> r.cookies #返回cookie
>>> r.history #返回重定向信息, of course, can be added allow_redirects = False in the request to prevent redirection

9 Timeouts

>>> r = requests.get (' url ', timeout=1) #设置秒数超时, only valid for connection

10 Session object that allows you to persist certain parameters across requests

>>> s = requests. Session ()
>>> S.auth = (' auth ', ' passwd ')
>>> s.headers = {' key ': ' Value '}
>>> r = s.get (' URL ')
>>> r1 = s.get (' url1 ')

11 agents

>>> proxies = {' http ': ' ip1 ', ' https ': ' IP2 '}
>>> requests.get (' url ', proxies=proxies)

Script Home Recommended reading:

Python requests installation and simple application

  • Related Article

    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.