python-requests

1、get和post请求

import requests  # 导入第三方库
# get
url = 'https://www.cnblogs.com/'
heads={"user-agent":"Mozilla/5.0"}
res = requests.get(url,headers=heads) # 返回一个消息实体(响应头、状态码、响应报文。。。)
print(res)  # 响应结果
heads={"user-agent":"Mozilla/5.0"} # 伪装浏览器标识
# 响应头 响应状态码 响应报文
print("响应头", res.headers)
print("响应状态码:", res.status_code)
print("响应正文:", res.text)
print("cookies:",res.cookies) # 类字典形式,可以根据key取值
print("代理user-agent",res.request.headers) # 请求头
# post
url = 'http://v.juhe.cn/laohuangli/d'
data = {"key": "XXXXXXXXXXXXXXX", "date": "2020-12-06"}
heads={"user-agent":"Mozilla/5.0"}
res = requests.post(url, data,headers=heads)
print("响应头:", res.headers)
print("响应状态码:", res.status_code)
print("响应正文:", res.json())
print("代理user-agent:",res.request.headers)
# html xml json -->text
# html xml json -->json()会报错,只有json类型的返回值才支持json

 

posted on 2020-12-05 21:14  Tester十点半  阅读(84)  评论(0编辑  收藏  举报

导航