Python http get and post usage, the difference between form submission and non-form submission

import requests

#get requestre
=requests.get('https://www.baidu.com/') #Request data through URL

print(re)
#Return <Response [200]> print(re.status_code) #Return the response code separately: 200
print(re.url) #Return the requested URL https://www.baidu.com/
print(re. text[:200])
#Return the first 200 characters of the requested content, the content has been decoded print(re.encoding) #Get the encoding type ISO-8859-1
print(re.content[:200]) #Get the undecoded content


#Add parameters for get request re=requests.get('https://www.baidu.com/s?wd=你好&rsv_spt=1') #Question mark? Add parameters later, you can add multiple parameters in a way like'wd=Hello&rsv_spt=1'


#Pass the parameters through the dictionary payload=('wd':'Hello','rsv_spt':1)
re=requests.get('https://www.baidu.com/s',params=payload)


#Pass the value twice to the same parameter through the field payload={'wd':'Hello','rsv_spt':[1,2]}
re=requests.get('https://www.baidu. com/s',params=payload)

#post requestre
=requests.post('https://www.baidu.com/s',data={'wd':'Hello','rsv_spt':1}) #here is the form submission method, It is displayed in the message as: wd=你好&rsv_spt=1, and other display methods in the message are non-form submission, such as {'wd':'Hello','rsv_spt':1}


#Extract the parameters payload=(('wd','nihao'),('rsv_spt',1)) #It is also the way of form submission, shown as "form" in the message: {"rsv_spt": "1 ","Wd": "nihao"}, where is the dictionary
re=requests.post('https://www.baidu.com/s',data=payload)

#Non-form submission parameters; the form method is mainly for the parameters of the dictionary or tuple mode, and the non-form is mainly for the parameter of the string type
import json

url='https://www.baidu.com/s'
payload={'wd':'nihao','rsv_spt':1}
re=requests.post(url,data=json.dumps(payload)) # Non-form submission parameters are displayed in the message as "data": "{"wd": "nihao", "rsv_spt": 1}", where it is the string
re=requests.post(url,json=payload ) #This is also a non-form submission, the json parameter automatically converts the payload content into a string

#requests的Other methods
re=requests.put('https://www.baidu.com/put',data={'wd':'Hello','rsv_spt':1})
#PUT request: if two Both requests are the same, and the latter request will overwrite the first request. (So ​​PUT is used to change the resource)
#Post request: The next request will not overwrite the first request. (So ​​Post is used to increase resources)

re=requests.delete('https://www.baidu.com/delete')
#Delete data re=requests.head('https://www.baidu.com/get')
#Get header information re=requests .options('https://www.baidu.com/get') #Get which method request the server supports

r=requests.get('https://api.github.com/events',stream=True)
print(r.json()[0]) #Convert the returned string into a list or dictionary, take the first One element
r.raw.read #The returned content is the binary data in the transmission process, bytes type data (binary data without encoding format)

with open('e:\a.txt','wb') as fd:
#Write the returned content into the file for i in r.iter_content(1000): #Write 1000 characters each time, write all in a loop , The content is not decoded, bytes type data (binary data with encoding type)
fd.write(i)

url='http://httpbin.org/post'
files={'file':('report.xls',open('e:\a.xlsx','rb'),'application/vnd.ms- excel',{'Expires':'0'})}
#'report.xls' indicates the name of the file sent to the server, open('e:\a.xlsx','rb') opens the local file, 'application/vnd.ms-excel' tells the server that the file is in excel format,'Expires': '0' means it does not expire
r=requests.post(url,files=files)
#send a file content to the server r.status_code # Return a status code
r.status_code==requests.codes.ok #Judging whether the status code is 200, if I am, return True
r.raise_for_status() #When the returned status code is not 200, throw an exception
r.headers #Get Response header information, stored in a dictionary
r.headers['Content-Type']
#Get the value corresponding to'Content -Type' r.headers.get('content_type')

url='https://www.baidu.com'
r=requests.get(url)
print(r.cookies) #Get the server's cookie list and store it in a dictionary
print(r.cookies['BDORZ']) # Get the value of the cookie named'BDORZ'
for k,v in r.cookies.items():
print(k,v) #Get the key and value of cookies

cookies=dict(a='zhangsan',b='lisi')
r=requests.get(url,cookies=cookies) #send cookies information to the server

requests.get(url,timeout=3) #Connection timeout setting, an error will be reported if more than three seconds

r=requests.head('http://github.com',allow_redirects=True)
#Redirect , if allow_redirects=False, prohibit redirection print(r.url) #Redirect to get the URL https://github.com /
print(r.history[0].url) #URL before redirect http://github.com/
print(r.history) #Response code object[<Response [301]>]

s=requests.Session()
#Transfer cookies across the request body, which can store the cookies obtained to the server s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') #Get
from the previous request The
received cookie is sent to the next URL r=s.get('http://httpbin.org/cookies')

s=requests.Session()
#Transfer cookies across request subjects, which can store cookies obtained to the server s.auth=('user','pass')
#If the webpage requires a username and password, you can set s.headers here .update({'a':'zhangsan'})
#Add a default information to all request headers r=s.get('http://httpbin.org/headers',headers={'b': 'lisi'}) #Add a request header to this request

Guess you like

Origin blog.csdn.net/weixin_44123630/article/details/113417637