current position:Home>Analysis of Python requests module
Analysis of Python requests module
2022-01-30 21:07:47 【Little cute in the circle of friends】
This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge
Review
Python Object oriented high level language . We have learned before Python It also supports the network request module .
- urllib modular On the basic url Request to provide request、error、parse、robot.txt Module method
- urllib3 modular Provide thread safe pool management and proxy service related methods
In this issue , We'll learn about the... Used at the bottom urllib3 Module's third-party network request Python modular -requests,Let's go~
1. requests The module overview
requests Modules in On its official website It says “ Give Way HTTP Serving humanity ”. therefore ,requests The module also adheres to this principle , Provide more convenient HTTP Request method .
In our code HTTP When you ask , If not handled properly, there will be security vulnerabilities 、 Code redundancy and other problems
-
requests Module features
-
The bottom floor is adopted, and the interior is adopted urllib3 Modules are packaged , More user-friendly and convenient
- We can see by looking at the source code requests Modules are indeed imported urllib3 Modular
-
Allow users to communicate in different ways HTTP Link request , Than urllib2 Module operation is more concise
-
Provide keep-alive And connection pool , Automation remains connected
-
Support cookie Keep talking
-
Support file upload 、 Automatic response content encoding
-
-
requests Module acquisition
-
Mode one : Use pip Direct download requests modular
pip install requests Copy code
-
Mode two : Use git Tool execution clone download
git clone git://github.com/kennethreitz/requests.git Copy code
-
-
requests Module USES
We need to use... In our code import Import requests modular
import requests Copy code
2. requests Related methods
requests Module is used Apache2 licensed Permit HTTP library , In the user manual, we can see that the main methods are as follows
Method | effect |
---|---|
requests.get(url) | send out get request |
requests.post(url) | send out post request |
requests.put(url) | send out put request |
requests.delete(url) | send out delete request |
requests.head(url) | send out head request |
requests.options(url) | send out options request |
requests The module provides a description of the request method field
Field | explain |
---|---|
url | Network request link |
data | Requested data , Data can be dictionaries 、 Tuples |
param | Pass on... In the form of a dictionary URL Parameters |
header | HTTP Request header |
files | Multiple encoded files |
allow_redirects | Redirect |
timeout | Overtime |
requests Module object attribute description
Field | explain |
---|---|
text | What the server returned |
encoding | Encrypted format |
content | The content of the request response |
json | Json Decode the content of the response |
raw | The content of the server's original response ( Binary system ) |
status_code | Response status code |
raise_for_status | Abnormal status code |
headers | Response header information |
cookie | Responsive cookie Information |
3. requests Starting steps
requests Used at the bottom of the module urllib3 modular , Highly encapsulate the requested method , Let's summarize the operation steps :
-
Import requests modular
import requests Copy code
-
Define a requests object , Such as calling get Request Nuggets website
r = requests.get("https://juejin.cn/user/211521683863847/posts") Copy code
-
Query the relevant information in the request response
print("status_code",r.status_code) print("content",r.content.decode("utf-8")) print("headers['Content-type']:",re.headers['Content-type']) Copy code
4. A profound
according to requests The request method provided by the module , Let's implement a working scenario :
Gave a si no interface , Let you test :
Interface :/action/openapi/post_list Interface parameters :
Parameters | Mandatory | Type and scope | explain | The default value is |
---|---|---|---|---|
access_token | true | string | oauth2_token Acquired access_token | |
catalog | false | int | Category ID 1- Question and answer 2- Share 3-IT Chowder ( comprehensive ) 4- depot 100- Career 0- all | 0 |
tag | false | string | Post related tags | |
pageSize | false | int | Number of entries per page | 20 |
page/pageIndex | false | int | the number of pages | 1 |
dataType | true | string | Return data type ['json' | 'jsonp' |
Interface request method :POST
First , We follow the interface request mode , Create a do_post Method
def do_post(ip,api,data):
headers = {"Content-Type": "application/json"}
url = "http://"+ip+api
print (url)
try:
r = requests.post(url,headers = headers,data = json.dumps(data),timeout= 10)
print(r.status_code)
print(r.content.decode("utf-8"))
except requests.exceptions.ConnectionError:
requests.status_code = "Connection refused"
except requests.exceptions.ReadTimeout:
requests.status_codes = "Read timed out"
Copy code
testing procedure :
data = {}
api_pose = ""
do_post(ip,api_pose,data)
Copy code
summary
In this issue , We are right. requests Module requests methods and related attributes for learning ,requests Module is based on urllib3 encapsulate , Give Way HTTP It's more convenient to ask .
The above is the content of this issue , Welcome big guys to praise and comment , See you next time ~
copyright notice
author[Little cute in the circle of friends],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302107441221.html
The sidebar is recommended
- Introduction to python (IV) dynamic web page analysis and capture
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
guess what you like
-
This is what you should do to quickly create a list in Python
-
On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
-
Python OpenCV image area contour mark, which can be used to frame various small notes
-
How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
-
Initial Python tuple
-
Introduction to Python urllib module
-
Advanced Python Basics: from functions to advanced magic methods
-
Python Foundation: data structure summary
-
Python Basics: from variables to exception handling
-
Python notes (22): time module and calendar module
Random recommended
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
- Parameter passing of Python function
- Stroke tuple in Python
- Talk about ordinary functions and higher-order functions in Python
- [Python data acquisition] page image crawling and saving
- [Python data collection] selenium automated test framework
- Talk about function passing and other supplements in Python
- Python programming simulation poker game
- leetcode 160. Intersection of Two Linked Lists (python)
- Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
- Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems