requests的使用(一)

423 阅读1分钟

响应代码

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) # 网页内容  
print(type(r.content))  # <class 'bytes'>
print(r.text)   # 网页内容
print(type(r.text))    # <class 'str'>
print(r.history)      # []
print(r.encoding)     # ISO-8859-1
print(r.is_redirect)  # 是否重定向 False
print(r.links)    # 子链接 {}

get方式的代码

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)
# 标准解析格式
print(req.content.decode("utf8","ignore"))

# json解析获得字典
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'>

上面标准化后的数据的样子

{
  "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"
}

post方式的代码

注意post方式中传递的参数是data,而get 是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"))

标准化后的数据的样子

{
  "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"
}