编程知识 cdmana.com

Use of request (1)

Code de réponse

import requests
r = requests.get("http://www.baidu.com")

print(type(r))  # <class 'requests.models.Response'>
print(r.status_code) # 200
print(r.url)     # http://www.baidu.com/
print(r.headers)   #{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Thu, 25 Nov 2021 08:56:15 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:27:52 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}
print(r.cookies)   # <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
print(r.content) # Contenu de la page 
print(type(r.content))  # <class 'bytes'>
print(r.text)   # Contenu de la page
print(type(r.text))    # <class 'str'>
print(r.history)      # []
print(r.encoding)     # ISO-8859-1
print(r.is_redirect)  # Redirection ou non False
print(r.links)    #  Sous - Liens  {}
Copier le Code

get Code du mode

import requests

data = {"name":"waws","age":18}
headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);"}
url = "http://httpbin.org/get"
req = requests.get(url,params=data,headers=headers)
#  Format de résolution standard 
print(req.content.decode("utf8","ignore"))

# json Analyser pour obtenir un dictionnaire 
print(req.json())  # {'args': {'age': '18', 'name': 'waws'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);', 'X-Amzn-Trace-Id': 'Root=1-619f5084-5a1b4b6a2c4332e12ee045ab'}, 'origin': 'xxx.xxx.xxx.xx', 'url': 'http://httpbin.org/get?name=waws&age=18'
print(type(req.json()))  # <class 'dict'>
Copier le Code

À quoi ressemblent les données normalisées ci - dessus

{
  "args": {
    "age": "18", 
    "name": "waws"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);", 
    "X-Amzn-Trace-Id": "Root=1-619f5084-5a1b4b6a2c4332e12ee045ab"
  }, 
  "origin": "xxx.x.xxx.xxx", 
  "url": "http://httpbin.org/get?name=waws&age=18"
}
Copier le Code

post Code du mode

Attention!post Le paramètre passé dans le mode est data,Etget - Oui.params

import requests
data = {"user":"admin","pwd":"admin"}
headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);"}
url = "https://httpbin.org/post"
req = requests.post(url,data=data,headers=headers)
print(req.content.decode("utf8","ignore"))
Copier le Code

L'apparence des données normalisées

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "pwd": "admin", 
    "user": "admin"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "20", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0);", 
    "X-Amzn-Trace-Id": "Root=1-619f5125-26e1e1e526026c547bb269ed"
  }, 
  "json": null, 
  "origin": "xxx.xxx.xxx.xx", 
  "url": "https://httpbin.org/post"
}
Copier le Code

版权声明
本文为[Waws520]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/11/20211125174841673c.html

Tags use request
Scroll to Top