A:
Html demande de page Web
import urllib.request
r=urllib.request.urlopen('http://httpbin.org/get')
print(type(r))
<class 'http.client.HTTPResponse'>
Le procédé ci-dessus en utilisant les résultats urlopen bibliothèque de modules de demande de urllib.
Ce qui suit est une demande de module méthode get.
import requests
r=requests.get('http://httpbin.org/get')
print(type(r))
<class 'requests.models.Response'>
Si la page est retournée au format JSON, vous pouvez appeler la méthode JSON (), vous pouvez retourner le résultat est un format JSON chaîne est converti dictionnaire.
import requests
r = requests.get(” http://httpbin.org/get”)
print(type(r.text))
print(r.json())
print ( type(r.json()))
<class ’ str ’ >
{'headers': {'Accept-Encoding ':’gzip, deflate', ' Accept' : '*I * ’,’Host' : 'httpbin. org', 'User-Agent ’ .
’ p y thon-requests /2 . 10.0 ’},’ url ’:’ http://httpbi「1. org/get ’,’ args ’:{},’origin ’:’ 182.33.248.131 '}
<class ’dict ’>
Si le retour est pas au format JSON avec la méthode JSON () se plaindra.
import requests
r=requests.get('https://www.d4j.cn/')
print(r.json())
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
deux:
acquisition de données binaires
Obtenir la photo:
import requests
r=requests.get('https://www.d4j.cn/wp-content/uploads/2019/09/touxiang1.jpg')
with open('touxiang1.jpg','wb') as f:
f.write(r.content)
L'obtention d'un audio:
import requests
r=requests.get('https://m10.music.126.net/20200311095903/04035a624339d0162516f5b54a0458bd/yyaac/obj/wonDkMOGw6XDiTHCmMOi/1631712277/7d44/28fe/7d74/cb614488c90af705a440a4c5332112d8.m4a')
print(type(r))
with open('pachong.m4a','wb') as f:
f.write(r.content)
trois:
Ajouter aux en-têtes de demande.
import requests
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
r=requests.get('https://www.zhihu.com/explore',headers=headers)
print(r.text)
quatre:
Utiliser les requêtes POST
import requests
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
data={'name':'lingxiaoyun','age':'22'}
r=requests.post('http://httpbin.org/post',data=data,headers=headers)
print(r.text)
cinq:
Nature de la réponse de divers
import requests
r=requests.get('https://www.jianshu.com/')
print(type(r.status_code))<class 'int'>
print(type(r.headers))<class 'requests.structures.CaseInsensitiveDict'>
print(type(r.cookies))<class 'requests.cookies.RequestsCookieJar'>
print(type(r.url))<class 'str'>
print(type(r.history))<class 'list'>
six:
demande offre également un objet de requête code d'état intégré requests.codes,
La comparaison peut prendre le code d'état retourné et le code d'état intégré.
r.status_code==requests.codes.+对应状态码英文
sept:
Uploader
import requests
files={'file':open('touxiang1.jpg','rb')}
r=requests.post('http://httpbin.org/post',files=files)
print(r.text)
{
"args": {},
"data": "",
"files": {
"file": "data:application/octet-stream;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAgICAgJCAkKCgkNDgwODRMREBARExwUFhQWFBwrGx8bGx8bKyYuJSMlLiZENS8vNUROQj5CTl9VVV93cXecnNEBCAgICAkICQoKCQ0ODA4NExEQEBETHBQWFBYUHCsbHxsbHxsrJi4lIyUuJkQ1Ly81RE5CPkJOX1VVX3dxd5yc0f/CABEIAV4BXgMBIgACEQEDEQH/xAAcAAEAAgIDAQAAAAAAAAAAAAAABgcEBQECAwj/2gAIAQEAAAAAv8AAAAAAAAAAAAAAAAAxtNu/XsAAAAAAIxQcuuLZgAAAAACE1lJtFePvy6nPYAAAABHqPkWy5tjHgccwO9xbUAAAAB5fP+b7a606m2vpi49+ewAAAADppal1HtDJ/kare3Dk9gAAAADjjTUtsJP0nW2AAAAAA48/mX6O2IAAAAAA40NGfSHYAAAAAAcV9Ebv5AAAAAAHWiplYjkAAAAAA6fMl7ybkAAAAAAY3y19LbgAAAAAANb8ufUO0AAAAAAHGo+Yvpn249Mr37AAAAAOry0ugpez/cZG4mPIAAAAeOj02r6I9u/Y57Zs/wC4AAAA4ikZc5GXl5mT6ZW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nGlxL78gG5ofWDOxIilQMqZrh1qnwiUUGVIpAycvAxSBYYkCIo9BUbmaHooOFNIrIRgqRkYqztYnedGlwqx5UUVM0cCI5XoSo/wDBr//EACERAAICAgEEAwAAAAAAAAAAAAABAkARIRIDEzEyEFCA/9oACAECAQE/APtEIZj41SQ3WltpCqr2rLzVfgj7OplEpoU9s7jO4KadBzNnFsXTYoHAUMUML8A//8QAJhEAAgEEAQMDBQAAAAAAAAAAAQIAAwQRQDESE0EFITIiQlFSgP/aAAgBAwEBPwDeJwCZTu6buQG1W4IhdqN06n9jLG5FanhvkISByY9dE8iLchyAuNK+9LFZzUpnBllb3dG4AKnH5l7VrUshFYsfMS2vLnnq9/JlnYi2X6jltMeTDg6v2azfHVHMfgamDFQ5EZAQJ2hDTEKaCqMQdInWIagncE7kLnQyZn+AP//Z"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "6587",
"Content-Type": "multipart/form-data; boundary=f1c122b2b66b7ff7cd2fb863746bea81",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.23.0",
"X-Amzn-Trace-Id": "Root=1-5e6a1a5a-eb33730e81ad596002b15563"
},
"json": null,
"origin": "120.239.40.206",
"url": "http://httpbin.org/post"
}
huit:
Obtenez les cookies
import requests
r=requests.get('http://www.baidu.com')
print(r.cookies)
for key,value in r.cookies.items():
print(key+'='+value)
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
BDORZ=27315
Vous pouvez également copier directement sur la page.
#用cookies维持模拟登录状态
import requests
headers={
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36' ,
'cookie':'_zap=3d265c1d-a192-40ae-b161-ebbdcfbc5de5; d_c0="ANDbo3r35hCPTlutBRPg1B1PlU0z9dIwk24=|1583157820"; _ga=GA1.2.665609525.1583157823; _xsrf=EVP0fTWfL0GSZPNLIW8VJ5l2ZtAzHLim; _gid=GA1.2.271137614.1583896687; capsion_ticket="2|1:0|10:1583896794|14:capsion_ticket|44:ZmJjZWU3NmRjMjI3NDNjNGFiMDM1MzEyMzQ0YjFjYTk=|e4c0624ebeb242170a53e8a54bea8a3dfe8bcc13f6eb570320b70075fbc67fd9"; z_c0="2|1:0|10:1583896815|4:z_c0|92:Mi4xcEdHU0VBQUFBQUFBME51amV2Zm1FQ1lBQUFCZ0FsVk43NlpWWHdBYlYwZ0QxZHRoVTBYUHNGNG95MDFiRnQtQmNB|0a04f55bbaeabd1a3965d304b6721c81a3f1f47d2e5a71586bf278babab30a87"; tst=r; q_c1=978b2b08310547e0a04c7aa973618e49|1583897071000|1583897071000; _gat_gtag_UA_149949619_1=1; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1583637094,1583637203,1583896687,1583979866; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1583979881; KLBRSID=53650870f91603bc3193342a80cf198c|1583979881|1583979747'
}
response=requests.get('https://www.zhihu.com',headers=headers)
print(response.text)
neuf:
Session de maintenir
Avant la session ne se maintient pas, le programme est l'équivalent de deux différentes demandes, un autre navigateur. Bien que les cookies peuvent être utilisés pour maintenir, mais plus compliqué, on peut facilement utiliser l'objet de la session pour maintenir la session.
import requests
requests.get('http://httpbin.org/cookies/set/number/123456')
r=requests.get('http://httpbin.org/cookies')
print(r.text)
{
"cookies": {}
}
Ici, avec Session:
import requests
s=requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456')
r=s.get('http://httpbin.org/cookies')
print(r.text)
{
"cookies": {
"number": "123456"
}
}
objet Session peut maintenir la même session, utilisez plus.
dix:
Ignorer les avertissements de certificat avant d'utiliser le 12306, mais maintenant les gens ont des informations d'identification, quelle que soit l'utilisation.
import requests
from requests.packages import urllib3
urllib3 . disable_warnings()
response = requests.get('https://www.12306.cn',verify= False)
print(response.status_code)
XI:
Paramètres du proxy
import requests
proxies={
'http':'',
'https':'',
}
requests.get('https://www.taobao.com',proxies=proxies)
12:
authentification
Lecture.
treize:
La demande est représenté comme une structure de données
Lecture.