Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Requests library in python

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what is the use of the Requests library in python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Introduction to 1.Requests

Request is the only Non-GMO HTTP library for Python and is safe for human use.

The only thing that cannot be bypassed in Python crawlers is the requests library. On the other hand, Requests citing urllib makes developers feel more user-friendly, more concise and more comfortable. The following features are extracted from the official Requests documentation:

Maintain vitality and connection pooling

Internationalized domain name and URL

Session with permanent Cookie

Browser-style SSL authentication

Automatic content decoding

Basic / digest authentication

Elegant key / value Cookie

Automatic decompression

Unicode response body

HTTP (S) Agent support

Uploading files in parts

Streaming download

Connection timeout

Block request

Supports .netrc

two。 Required installation

The request is the third-party library of python, so we need to install it using pip

Pip install requests

Or through binary installation

Git clone git://github.com/kennethreitz/requests.git cd python setup.py install3. Required use case

The commonly used HTTP operations are GET and POST. For other uncommonly used operations, you can refer to the official documentation or call the corresponding methods through the serial port.

Import requests# GET request response = requests.get ("https://getman.cn/echo")print(response.text)# GET constructs header,cookie, parameter request headers = {'user-agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', 'accept':' text/html,application/xhtml+xml,application/xml Qresume 0.9 imageCharpy AvifEng imageCompact webpDie imageUniverse apngRecyclActionAccording to qaccount0.8Gen ApplicationLexchangeThere is a sign sign for exchangeThere is a sign sign Cookie = {"user": "APython"} params = {'my_name':'AL','name':'APython'} response=requests.get ("https://getman.cn/echo",headers=headers,cookies=cookie,params=params)print(response.text)#POST request data= {' name':'APython-post','age': 24,} response= requests.post (" https://getman.cn/echo", data=data) print (response.text)) "

4. Request more sample import requests# download files (1) small file url = 'https://raw.githubusercontent.com/psf/requests/master/ext/ss.png'response = requests.get (url) with open (' demo.png') 'wb') as f: f.write (response.content) # download file (2) large file file_url = "https://readthedocs.org/projects/python-guide/downloads/pdf/latest/"response = requests.get (file_url) with open (" python.dpf ") "wb") as pdf: for chunk in response.iter_content (chunk_size=1024): if chunk: pdf.write (chunk) # POST submitted data returned result url = 'https://api.github.com/some/endpoint'data = {' some': 'APython'} response = requests.post (url) Data=data) print (response.text) # session session persistence (session objects can hold certain parameters across requests) session = requests.session () session.get (url) session.post (url,data) Thank you for reading this article carefully I hope the article "what is the use of Requests Library in python" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report