Python requests basic learning, pythonrequests
First, in the Python standard libraryUrllib2The module provides most of the HTTP functions you need, but its APIs are unfriendly. It was created for another age and another Internet. It requires a huge amount of work, or even various methods, to complete the simplest task, so learning the reuqests module is simple and easy to use (you will learn scapy and more powerful libraries later ), not to mention Installation
1. Import Module
>>> Import requests
2. intuitively understand the simplicity of sending requests
>>> R = requests. get ('your url ')
>>> R = requests. post ('your url ')
# The put delete head options method is also used.
3. Pass url parameters
>>> Url_params = {'key': 'value'} # The dictionary transmission parameter. If the value is None, the key is not added to the url.
>>> R = requests. get ('your url', params = url_params)
>>> Print (r. url)
Your url? Key = value
4. Response content
>>> R. encoding # obtain the current encoding
>>> R. encoding = 'utf-8' # sets the encoding.
>>> R. text # parse the returned content using encoding
>>> R. content # returned in bytes (Binary)
>>> R. json () # Return in json format. Ensure that the returned content is in json format. Otherwise, an exception is thrown when an parsing error occurs.
5. Custom header and coookie Information
>>> Header = {'user-agent': 'mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0 '}
>>> Cookie = {'key': 'value '}
>>> R = requests. get/post ('your url', headers = header, cookies = cookie)
6. Send a post Data Request
>>> Send = {'key': 'value '}
>>> R = requests. post ('your url', data = send) # If you passstring
Instead ofdict
, The data will be directly released, and this can also upload files
7. response status code
>>> R. status_code # if not 200, you can use r. raise_for_status () to throw an exception.
8. Response
>>> R. headers # The returned dictionary type and header information.
# R. requests. headers returns the header information sent to the server
>>> R. cookies # Return cookies
>>> R. history # returns the redirection information. You can addallow_redirects
= False: block redirection
9 timeout
>>> R = requests. get ('url', timeout = 1) # sets the time-out in seconds, which is only valid for connections.
10 Session object, allowing you to maintain certain parameters across requests
>>> S = requests. Session ()
>>> S. auth = ('auth', 'passwd ')
>>> S. headers = {'key': 'value '}
>>> R = s. get ('url ')
>>> R1 = s. get ('url1 ')
11 proxy
>>> Proxies = {'http': 'ip1', 'http': 'ip2 '}
>>> Requests. get ('url', proxies = proxies)