current position:Home>Python requests Library
Python requests Library
2022-01-31 08:15:14 【Stone sword】
grammar
requests.nameofmethod(parameters)
Copy code
Background and introduction
_requests_ How many libraries are there GET Methods . The first part and The second part It involves a lot in requests.get()
. The point of this article is _GET_ Other methods and error handling .
If it doesn't exist on the computer requests library , Please browse The first part Explanation .
"get " request " The header file "
By default , This value is None
. If True
, One HTTPS Head message Dictionaries It will be transmitted to the specified URL.
When one HTTP_ request _ When it was launched , One User-Agent The string is transmitted with the request , This string contains the following details of your system .
- Application type
- operating system
- Software supplier
- The software version of the requested user agent
The server uses these details to determine your computer's capabilities .
In this case , This code will send its header information to the server :
- The first [1] Line import
request
library - The first [2] A well formed User-Agent Save the string to _hdrs_ variable
- The first [3] Line trying to connect to _URL_, And set the header information to _hdrs_
- The first [4] Output the header information response to the terminal
- The first [5] Line closes the open connection
Code
import requests
hdrs = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/72.0.3626.121 Safari/537.36"}
response = requests.get('https://app.finxter.com', headers=hdrs)
print(response.headers)
response.close()
Copy code
Output
{'Server': 'nginx/1.14.0 (Ubuntu)', 'Date': 'Fri, 05 Nov 2021 16:59:19 GMT', 'Content-Type': 'text/html; charset=utf-8',
'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'X-Frame-Options': 'DENY', 'Vary': 'Cookie', 'X-Content-Type-Options': 'nosniff', 'Set-Cookie': 'sessionid=0fb6y6y5d8xoxacstf74ppvacpmt2tin; expires=Fri, 19 Nov 2021 16:59:19 GMT; HttpOnly; Max-Age=19600; Path=/; SameSite=Lax', 'Content-Encoding': 'gzip'}
Copy code
Be careful : This is a great Python function . If you are right about Web Scraping Interested in , You may want to delve further into this topic .
"get " request " Agency "
If you're an avid web Raider , Or you need to keep your privacy online , Using agents is the answer . The agent will your IP The address is hidden from the outside world .
In this case , We get a new... From a free agency service IP Address , And add it to a dictionary .
- The first [1] Line import
request
library - The first [2] Set a row URL To _the_url_ Variable
- The first [3] Line adds a new proxy in the form of a dictionary , Up to now
- The first [4] Line attempts to connect to the _URL_, And set the proxy to _my_proxy_
- The first [5] Output the status code response to the terminal
- The first [6] Line close open connection
Code
import requests
the_url = 'https://somewebsite.com'
my_proxy = {"https": "https:157.245.222.225:3128"}
response = requests.get(the_url, proxies=my_proxy)
print(response.status_code)
response.close()
Copy code
Output
200
Copy code
"get" request ." flow "
This method is unnecessary . By default , This value is False
. If False
, The response transmission indicates that the file should be downloaded immediately . If it is True
, Then stream the file
- The first [1] Line import
request
library - The first [2] On the way _URL_ Set to logo Location , And will stream Set to _True_
- The first [3] The line outputs the status code in response to the terminal
- The first [4] Line close open connection
Code
import requests
response = requests.get('https://app.finxter.com/static/favicon_coffee.png', stream=True)
print(response.status_code)
response.close()
Copy code
Output
200
Copy code
exception handling
There are a lot of exceptions and request
'_s_ Library related
There are two ways to deal with this situation
individualization
In this case , We will Overtime To requests.get()
. If the connection or server times out , An exception will occur :
- The first [1] Line import
request
library - The first [2] Line initialized _try_ sentence . The code here will first run
- The first [3] Line trying to connect to URL And set timeout
- The first [4] Output the status code to the terminal
- The first [5] The line closes the open connection
- The first [6] Line is _except_ sentence . In case of _ Overtime _, The code will fall here
- The first [7] The line outputs information to the terminal _Timed Out!_ The script is over
Code
import requests
try:
response = requests.get('https://app.finxter.com', timeout=(2,4))
print(response.status_code)
response.close()
except requests.ConnectTimeout():
print('Timed Out!')
Copy code
Output
200
Copy code
All exceptions
_ request _ All exceptions in the library are inherited from requests.exceptions.RequestException. For this example , This code captures all exceptions
- The first [1] Line import
request
library - The first [2] Line initialized _try_ sentence . The code here will first run
- The first [3] Line trying to connect to URL And set timeout
- The first [4] Output the status code to the terminal
- The first [5] The line closes the open connection
- The first [6] Line is _except_ sentence , If anything goes wrong , The code will fall here
- The first [7] Line exception information (e) Output to terminal , Script end
Code
import requests
try:
response = requests.get('https://app.finxter.com', timeout=(2,4))
print(response.status_code)
response.close()
except requests.exceptions.RequestException as e:
print(e)
Copy code
Output
200
Copy code
in addition
You can also convert the above content into a reusable function , By modifying this code to meet different needs
Code
def error_code(url):
try:
response = requests.get('https://app.finxter.c', timeout=(2,4))
except requests.exceptions.RequestException as e:
return e
nok = error_code('https://app.finxter.com')
print(nok)
Copy code
summary
In this article , We learned how to .
- Use header file
- Use an agency
- Use stream
- Implement exception handling
copyright notice
author[Stone sword],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310815114871.html
The sidebar is recommended
- Django ORM details - fields, attributes, operations
- Python web crawler - crawling cloud music review (3)
- Stroke list in python (bottom)
- What cat is the most popular? Python crawls the whole network of cat pictures. Which one is your favorite
- [algorithm learning] LCP 06 Take coins (Java / C / C + + / Python / go / trust)
- Python shows the progress of downloading files from requests
- Solve the problem that Django celery beat prompts that the database is incorrectly configured and does not support multiple databases
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python quantitative data warehouse construction 3: data drop library code encapsulation
- The source code and implementation of Django CSRF Middleware
guess what you like
-
Python hashlib module
-
The cover of Python 3 web crawler development (Second Edition) has been determined!
-
The introduction method of executing Python source code or Python source code file (novice, please enter the old bird and turn left)
-
[Python basics] explain Python basic functions in detail, including teaching and learning
-
Python web crawler - crawling cloud music review (4)
-
The first step of scientific research: create Python virtual environment on Linux server
-
Writing nmap scanning tool in Python -- multithreaded version
-
leetcode 2057. Smallest Index With Equal Value(python)
-
Bamboolib: this will be one of the most useful Python libraries you've ever seen
-
Python crawler actual combat, requests module, python realizes capturing a video barrage
Random recommended
- [algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)
- Test platform series (71) Python timed task scheme
- Java AES / ECB / pkcs5padding encryption conversion Python 3
- Loguru: the ultimate Python log solution
- Blurring and anonymizing faces using OpenCV and python
- How fast Python sync and async execute
- Python interface automation test framework (basic) -- common data types list & set ()
- Python crawler actual combat, requests module, python realizes capturing video barrage comments of station B
- Python: several implementation methods of multi process
- Sword finger offer II 054 Sum of all values greater than or equal to nodes | 538 | 1038 (Java / C / C + + / Python / go / trust)
- How IOS developers learn python programming 3-operator 2
- How IOS developers learn python programming 2-operator 1
- [Python applet] 8 lines of code to realize file de duplication
- Python uses the pynvml tool to obtain the working status of GPU
- Data mining: Python actual combat multi factor analysis
- Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency
- Use Python VTK to batch read 2D slices and display 3D models
- Complete image cutting using Python version VTK
- Python interface automation test framework (basic) -- common data types Dict
- Django (make an epidemic data report)
- Python specific text extraction in actual combat challenges the first step of efficient office
- Daily python, Part 8 - if statement
- Django model class 1
- The same Python code draws many different cherry trees. Which one do you like?
- Python code reading (Chapter 54): Fibonacci sequence
- Django model class 2
- Python crawler Basics
- Mapping 3D model surface distances using Python VTK
- How to implement encrypted message signature and verification in Python -- HMAC
- leetcode 1945. Sum of Digits of String After Convert(python)
- leetcode 2062. Count Vowel Substrings of a String(python)
- Analysis of Matplotlib module of Python visualization
- Django permission management
- Python integrated programming -- visual hot search list and new epidemic situation map
- [Python data collection] scripy realizes picture download
- Python interface automation test framework (basic part) -- loop statement of process control for & while
- Daily python, Chapter 9, while loop
- Van * Python | save the crawled data with docx and PDF
- Five life saving Python tips
- Django frequency control