In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the role of the Requests library in Python is, the content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. Import the requests library:
Import requests
two。 Let's first talk about two get request methods in the requests library:
-the first one:
# request using get
Rsp = requests.get (url)
-the second type:
# request using request
Rsp1 = requests.request ("get", url)
Of course, the parameters passed in are not only url, but also params provides these parameters in a string dictionary, and sends the parameters in the dictionary to the specified web page. In addition, like request, you can set headers:
Url = "http://www.baidu.com/s?"
Kw = {
'wd': 'Panda
}
Headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
# request using get
Rsp = requests.get (url, params=kw, headers=headers)
3. We can also read the contents of the server response through requests
R = requests.get ('https://api.github.com/events')
Print (r.text)
Print the content, here are a variety of parameters returned by the server (it will not be written because there are too many results returned)
4. Let's talk about requests's post.
Rsp = requests.post (url, data=data)
Just put the data you want to post into a dictionary, data, and then pass parameters directly. It's very concise here.
5. Let's talk about Cookie in requests.
-if a response contains some cookie, you can quickly access them and return a cookiejar:
R = requests.get ('https://api.github.com/events')
Cookiejar = r.cookies
# you can convert cookiejar to dictionary format
Cookiedict = requests.utils.dict_from_cookiejar (cookiejar)
-to send your cookies to the server, use the cookies parameter:
R = requests.get (url, cookies=cookies)
Cookie is a dictionary format, which can send parameters directly.
Session in 6.requests
-simulate a session, disconnecting from the client link server to the client browser
-We keep some parameters across requests, such as maintaining cookie between all requests issued by the same session instance
# create a session object to keep cookie values
Ss = requests.session ()
Headers = {'User-Agent':'xxxxxxxxxxxxxx'}
Data = {'name':' xxx'}
# the request is managed by the created session object
Ss.post ("http://www.baidu.com", data=data, headers=headers)
Rs = ss.get ('xxxxxxxxxx')
Proxy proxy in 7.requests
Proxy = {'http':' your agent ip',' https':' your agent ip'}
Rsp = requests.request ('get',' http://www.baidu.com', proxies=proxy)
-if your agent ip is purchased, there may be user authentication, which will use HTTP basic Auth
# format user name: password @ proxy address: Port
Proxy = {'http':'china:123456@192.168.1.1:3006'}
Rsp = requests.get ("http://www.baidu.com", proxies=proxy)
-web client authentication
If you encounter web client authentication, you need to add auth= (username, password)
Auth = ("123", "3333")
Rsp = requests.get ("http://www.baidu.com", auth=auth) what is the role of the Requests library in Python? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.