requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到, Requests是Python语言的第三方的库,专门用于发送HTTP请求
pip install requests
import requests class testDemo(): def testClass(self): # 使用get测试的第一种方式 r="http://apis.juhe.cn/simpleWeather/query?city=%E5%8C%97%E4%BA%AC&key=d69c928f3828935873d001e1faa0040c" a=requests.get(r) # 查看url print(a.url) print("-----------------") # 查看状态码 print(a.status_code) print("-----------------") # 响应文本 print(a.text) print("-----------------") # 以二进制数返回 print(a.content) print("-----------------") # 当前编译格式 print(a.encoding) tt=testDemo() tt.testClass()
import requests class testDemo(): def testClass(self): url="http://apis.juhe.cn/simpleWeather/query" zd={"city":"北京","key":"d69c928f3828935873d001e1faa0040c"} zz=requests.get(url,params=zd) print(zz.text) tt=testDemo() tt.testClass()
import requests class testDemo(): def testClass(self): url="http://apis.juhe.cn/mobile/get?phone=17319197731&dtype=&key=ac6fc9689c6dcd90670753c71014e300" ff=requests.post(url) print(ff.text) tt=testDemo() tt.testClass()
import requests class testDemo(): def testClass(self): url="http://apis.juhe.cn/mobile/get" zd={"phone":"1739197731","key":"ac6fc9689c6dcd90670753c71014e300"} ff=requests.post(url,zd) print(ff.text) tt=testDemo() tt.testClass()
import csv class ReadDemoCsv(): def readCsv(self): item=[] rr=csv.reader(open("../csvtest/0607.csv","r")) for i in rr: item.append(i) return item[1:] rr=ReadDemoCsv() print(rr.readCsv())
from csvDataDemo.csvDemo import ReadDemoCsv rrr=ReadDemoCsv() lists=rrr.readCsv() import requests class requetbbb(): def ccc(self): itme=[] for i in lists: if i[2]=="get": rr=requests.get(i[0],params=i[1]) itme.append(rr.status_code) else: rr=requests.post(i[0],data=i[1]) itme.append(rr.status_code) return itme rc=requetbbb() print(rc.ccc())
import pytest from csvaaaa.requesttest import requetbbb rc=requetbbb() ff=rc.ccc() class TestClass(): def test001(self): for i in ff: assert i==200 if __name__ == '__main__': pytest.main(["test_001.py"])
import pytest,os,allure from csvaaaa.requesttest import requetbbb rc=requetbbb() ff=rc.ccc() class TestClass(): def test001(self): for i in ff: assert i==200 if __name__ == '__main__': pytest.main([ '--alluredir', 'report/result', 'test_001.py']) split = 'allure ' + 'generate ' + './report/result ' + '-o ' +'./report/html ' + '--clean' os.system(split)