requests library

# coding = utf-8
"""
same as urllib
requests is also a third-party library for sending http requests compatible with Python 2 and 3
It implements most of the functions of http.

install pip install requests
"""

# 1.requests send request 
"""
The same as directly with requests.get/post to specify the way to send the request
In the pycharm environment, you can enter the source code through alt+mouse click, and you can observe the relevant parameters of requests
""" 
# import requests #Request Baidu page 
# response = requests.get('http://www.baidu.com') 
# print(response) #Print 
the 
response content, you can also view the content contained in the response object through debug Value 
# The request return value is a Response object, which is the encapsulation of the response data returned by the server to the client in the HTTP protocol 
# including status code, response header, response body

#Display status code # code = response.status_code # Status 
code


#Response header information # for key, value in response.headers.items(): 
#      print('{}:{}'.format(key, value))


#Response body # print(response.content) 
# content returns the original data, byte string, bytes type, general picture, audio, video data, just use content 
# Can be decoded by response.content.decode(), default utf-8 Decode, convert bytestring to string

# print(response.text) #Return 
text data, str type, which is transcoded data, but the Chinese in the text may be garbled

#Display the encoding method of the requested webpage # print(response.encoding) 
# Usually ISO-8859


#Set the encoding method of the webpage # response.encoding = 'utf-8' 
#Because the default encoding of the webpage is ISO8859, it may be garbled when displaying Chinese. #So 
setting the encoding to utf-8 by the above method can solve the problem of Chinese garbled characters

# 2.requests request url pass parameter 
"""
By passing parameters to the url, the strings are concatenated, so the request becomes http://www.baidu.com/s?wd=python
"""
# import requests
# get请求的传参
# parms = {'wd': 'python'}
# response = requests.get('http://www.baidu.com/s?', parms=parms)

# The parameters of the post request, the url here is the test website 
# Same as get, the parameter type is also in dictionary format 
# data = {'key1': 2, 'key3': 3} 
# r = requests.post('http: //httpbin.org/post', data=data) 
# print(r.text)


# 3. Timeout setting in requests 
# import requests #Set the timeout time to 5 seconds, if the target address does not respond within 5s 
, an exception will be thrown 
# requests.get('https://www.google.com', timeout= 5)


# 4. Cookies processing in request 
"""
Request the target website through requests, and automatically carry cookies in the return value
Viewable via response.cookies
You can save the returned cookie and bring it with you next time you visit
example:
cookie = response.cookies
The next request: resquest.get(xxx.xxx, cookies=cookie) can carry the last visit
cookie information returned when the page
"""
# import requests
# params = {'wd':"python"}
# response = requests.get('http://www.baidu.com/s?', params=params)
# print(response.cookies)



# 5. Session in requests --session 
"""
The http protocol is a stateless protocol and cannot maintain the session state, so cookies appear, which carry cookie information when accessing,
You can keep the information of the last session, and the return value in requests carries cookies information, but it needs to be used for the next visit.
It is still inconvenient to pass in manually, so when a session appears, it will automatically save the cookies information.
and automatically pass in on the next visit
The way to access a web page is basically the same as requests
""" 
# import requests #Building 
a session api is basically the same as requests, and subsequent access operations are performed through the session 
# session = requests.session() #The 
request method is the same as request 
# session.get('http://www .baidu.com')


# 6. ssl authentication in requests 
"""
Some websites require a certificate when requesting, so the certificate needs to be processed
Set verify=False to set the certificate not to be verified when requesting a website
Similarly, verify the certificate on request by setting verify=True
"""
# import requests
# r = requests.get('https://www.jianshu.com', verify=False)

# 7.requests disguise the browser to visit the target website 
"""
Some websites will identify whether the request is made through code or a human being. Requests also provide disguise as
Browser interface, you can pass parameters through headers

Because Zhihu will automatically jump to the login page when you are not logged in
Do not jump by setting allow_redirects=False
"""
# import requests
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
# }
# r = requests.get('http://www.zhihu.com', verify=False,
#                  headers=headers, allow_redirects=False)

#
# 8. Proxy settings in requests 
# import requests #Set the proxy to the following format, and the parameters can be passed in 
# proxies = { 
#      'http': 'http://10.11.12.13:1234', 
# 
'      https': 'http://10.11.12.13:1234', 
# } 
# requests.get("http://httpbin.org/ip", proxies=proxies)

# 9. json
"""
Convert json format data into dictionary format, provided that the return value of the target website is in json format
Therefore, this method is rarely used
"""
# import requests
# url = 'http://www.baidu.com'
# r = requests.get(url)
# print(r.json())

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325401094&siteId=291194637