requests
requests
什么是requests?
Requests: 让 HTTP 服务⼈类 urllib urllib2 urllib3
下载方法:
pip3 install requests
基本应用
get请求
#! /usr/bin/env python # -*- coding:utf-8 -*- # author:晨黎 import requests import json
r=requests.get(
url="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo",
params={"mobileCode":"15*******73","userID":""}
)
print(r.text)
print(r.status_code)
print(r.headers)
print(r.url)
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">159*****373:陕西 榆林 陕西移动全球通卡</string>
200
{'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/xml; charset=utf-8', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Server': 'Microsoft-IIS/7.5', 'X-AspNet-Version': '2.0.50727', 'X-Powered-By': 'ASP.NET', 'Date': 'Wed, 19 Jan 2022 13:13:08 GMT', 'Content-Length': '271'}
post请求(json数据格式)
response=requests.post( url='http://47.95.142.233:8000/login/auth/', json={'username':'15929643373','password':'123456'}, headers={'content_type':'application/json'} ) print(json.dumps(response.json(),indent=True,ensure_ascii=False)) { "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiIzWlNKM0dkbTgydldDSlBKUE5pUXVrIiwiZXhwIjoxNjQyNjg0MTUxfQ.0LegK5J3pxW3vbNV6-an-GHsLSDsvQvPl59rFN7WwUM", "user": { "uid": "3ZSJ3Gdm82vWCJPJPNiQuk", "telephone": "159*******3", "username": "小陈", "email": null, "avator": "", "date_joined": "2022-01-09T11:14:06.114797+08:00", "is_active": true } }
响应数据⼀般使⽤r.text来获取,如果响应数据是JSON格式的,那么使⽤r.json()
post请求(表单数据格式)
r=requests.post( url="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo", data={"mobileCode":"159*****373","userID":""}, headers={"Content-Type":"application/x-www-form-urlencoded"} ) print(r.text) print(r.status_code) print(r.headers) print(r.url) <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://WebXml.com.cn/">159*****373:陕西 榆林 陕西移动全球通卡</string> 200 {'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/xml; charset=utf-8', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Server': 'Microsoft-IIS/7.5', 'X-AspNet-Version': '2.0.50727', 'X-Powered-By': 'ASP.NET', 'Date': 'Wed, 19 Jan 2022 13:15:58 GMT', 'Content-Length': '271'} http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo
动态参数处理
def login(): r=requests.post( url="http://47.95.142.233:8000/login/auth/", json={"username":"15929643373","password":"123456"}, headers={"Content-Type":"application/json;charset=UTF-8"} ) return r.json()["token"] print(login()) def index(): r=requests.get( url="http://47.95.142.233:8000/interface/index", headers={"Authorization":"jwt {token}".format(token=login())} # headers = {"Authorization": "JWT {0}".format(login())}) ) print(r.text) assert r.json()["count"]["api"]==4 index() eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiIzWlNKM0dkbTgydldDSlBKUE5pUXVrIiwiZXhwIjoxNjQyNjg2MDQ3fQ.2QoZFuSn8vx_FYcfjMfSZWXW7yFHamdlJDnIE4l0Q2w {"count":{"product":186,"api":4,"suite":3,"api_records":0,"suite_records":1}}
requests参数
headers ⾥⾯填写请求头的信息,请求头的信息都是字典的数据类型
headers={"Authorization":"JWT {token}".format(token=login())}
cookies 在请求头⾥⾯需要带上服务端的响应头中返回来的set-cookie的值
def login(): r=requests.post( url="https://home.51cto.com/index?reback=https%3A%2F%2Fhome.51cto.com&iframe=0&is_go_to_user_set_mobile=1", data={ "_csrf":"NFVxQUZHelUHPh8SDxRXPEASRCoeI1dtAhFBFTRqG2dcLRA7EhQWOA==", "LoginForm[username]":"15929643373", "LoginForm[password]":"asd888/==", "show_qr":0 }, headers={ "Content-Type":"application/x-www-form-urlencoded", "Cookie":"_uab_collina=164205936736540110402231; gr_user_id=0b1051d6-22b8-453b-9433-9b70c8031880; UM_distinctid=17e525d4fe544d-08ff15db089497-57b1a33-280000-17e525d4fe651c; _ourplusFirstTime=122-1-13-15-36-7; www51cto=C1948558B4C1E625FA951F0482CF373AFFmP; callback_api_url=https%3A%2F%2Fedu.51cto.com%2Fcenter%2Fuser%2Findex%2Flogin-success%3Fsign%3D4d65A1MHVAIBBQcAAQMGUwYMAlVSBgQCBlRTBAxYEENDERIAIhJWdBQHcVVVRBoFBltHCRoAX10XV3EHUQZR%26client%3Dweb; pub_cookietime=2592000; Hm_lvt_110fc9b2e1cae4d110b7959ee4f27e3b=1642064960; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2215481258%22%2C%22first_id%22%3A%2217e525d842dd89-0cef705b327193-57b1a33-2621440-17e525d842e5fa%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2217e525d842dd89-0cef705b327193-57b1a33-2621440-17e525d842e5fa%22%7D; ssxmod_itna=YqIhqjx0EG8GCDzxAxeTj+IqDQAFqxym4rmbqKDsaEDSxGKidDqxBWmQoHWyiq5PDUrPiT1Y28G4tfifGaPhYWcmyPDHxY=DU1Dq+YD4RKGwD0eG+DD4DWeqAoDexGpc2pXKGWD4qDODWKDX2akDiPCDmR8pxGClxDC0APDwx0CLovF=4YpDi5xRarPLxG1F40H=G35LxOfLRh1RPSARQGiDBb5gt35dcd54Hr+fWIG440OD0KGLxibCrOUUhH+ZQhTTexNdPrqjiiP3l0eCiGKr70NNYRe4pRDFYnXqQ0q4hxySuxDiE09U+DD=; ssxmod_itna2=YqIhqjx0EG8GCDzxAxeTj+IqDQAFqxym4rmbqikEaeYDlZlQDj4R51dQidqDyiLye8Uz6LaimaB7lrivOQFErRhaz7Wri6aIm=ovCli9LFPsbL1eBgIoEK6HiuAqsQ58gyw8X4/Hfa0syP9DNnGWxrD=gP7tYUu+r3IrtKvmnagCnQ0D83HPzzyz37eIN88bWbBsZ=AsMcAf6g8C/PK6f9r+Bb0dcF30lIwHcIy6L4k==0mLQVLUYSCUy6xG3buuf2P8q6qWpfQf0SHk0U7WmVv8Yioi5OP8XLe8F+IBEXOkb8PQlXzi+ycDPQAbN8Hx3BC8yWzrPGaqB9dnRtj0s0eSGrsQwKq4Ct9j3cAGwnIa1nH5xcoN4aSD+Wqx3vvlKxmAy/hMWeSUHDkpU3If0+8GqQUoUBgrAT3D07QxDLxD2BGDD===; acw_tc=2760826e16425819472683730e7ccb62d1a2e5ecf2d243cb7ca6512d3d00bf; _csrf=f9304a4be54f06b4d86271a1c228ac505eae8e8adad5a1f665dae880db8c8bf6a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%223knSIS-itG5kXd-86D0Tr-a2hxazTSlm%22%3B%7D; Hm_lvt_844390da7774b6a92b34d40f8e16f5ac=1642061627,1642064984,1642086473,1642581950; pub_wechatopen=aG0wVVNRBVcLBgIFVA; PHPSESSID=6ri0nds9gb9ors8s4obs7sdhd6; once_p=732988; _ourplusReturnCount=41; _ourplusReturnTime=122-1-19-16-49-55; login_from=home.51cto.com; reg_from=home.51cto.com; Hm_lpvt_844390da7774b6a92b34d40f8e16f5ac=1642582196", "Referer":"https://home.51cto.com/index", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36", } ) print(r.text) print(r.status_code) return r.cookies print(login()) def homepage(): r=requests.get( url="https://edu.51cto.com/center/user/info/get-user-info",
cookies=login() ) print(r.text) print(r.status_code) homepage()
组件
auth 权限验证
from requests.auth import HTTPBasicAuth r=requests.get( url="http://101.43.158.84:5000/v1/api/books", auth=HTTPBasicAuth(username="admin",password="admin",), ) print(r.status_code) print(r.json()) 200 {'datas': [{'author': 'wuya', 'done': True, 'id': 1, 'name': 'Python接口自动化测试实战'}, {'author': '无涯', 'done': False, 'id': 2, 'name': 'Selenium3自动化测试实战'}], 'msg': 'ok', 'status': 0}
timeout
主要指的是服务端响应数据很慢,但有时候很快,所以这个时候建议加timeout 还有⼀种就是当⽹络请求出现requests.exceptions.ConnectTimeout的错误时候,那么就意味着该参数需要上场 了
from requests.auth import HTTPBasicAuth r=requests.get( url="http://101.43.158.84:5000/v1/api/books", auth=HTTPBasicAuth(username="admin",password="admin",), timeout=30 )
verify
当你的代码没任何的问题,但是请求就是返回ssl 以及403的问题,那么就意味着需要设置verify的参数 如返回错误:requests.exceptions.SSLError。那么就需要带上该参数
r=requests.get( url='https://101.43.158.84:5000/v1/api/books', auth=HTTPBasicAuth(username="admin",password='admin'), timeout=180,verify=False) print(r.status_code) print(r.json())