Requests is a useful PythonHttp client library that is often used when writing crawlers and testing server responses. Request is a third-party library in Python. GET r=requests. GET (‘ www.baidu.com ‘) Payload ={‘ key1 ‘:’ valuea ‘, ‘key2’ : ‘value2’, ‘key3’ : ‘None’}
Unable to import requests packages, type the PIP install requests command in PyCharm to download themCopy the code
Three ways to get data from the interface, demo.py
import requests from readDemo.readCsv import ReadCsv mReadCsv = ReadCsv() aaa = mReadCsv.read_csv() class BasiDu(): # def method(self): # 1, # r = requests. Get ("http://www.baidu.com") # print(r.tatus_code)# print(r.ext)# print(r.ext)# Print (r.healers) # def method2(self): # 2, get real interface data # 1: Concatenate the parameters directly to the interface # r = requests.get("http://web.juhe.cn:8080/environment/air/cityair?city=shanghai&key=1e75c6a18dff18d92560284d42cbaf50") # # # pas = {"city": "Shanghai ", "key": {"city":" Shanghai ", "key": "1e75c6a18dff18d92560284d42cbaf50"} # r = Requests. The get (" http://web.juhe.cn:8080/environment/air/cityair ", params = pas) # get request params # r = Requests. Post (" http://web.juhe.cn:8080/environment/air/cityair ", data = pas) # post request with data # # 3 to create a CSV file, Params = if aaa[1][2] in "get": r = requests. Get (aaa[1][0], params=aaa[1][1]) else: R = requests. Post (aaa[1][0], data=aaa[1][1]) print(r.tatus_code)Copy the code
readCsv.py
import csv # readCsv.py class ReadCsv(): def read_csv(self): item = [] r = csv.reader(open(".. /demoDemo/user.csv", "r")) for csv_i in r: Return item # # r = ReadCsv() # a = r.read_csv() # print(a)Copy the code
user.csv
Url, params,method http://web.juhe.cn:8080/environment/air/cityair,{"city":"shanghai","key":"1e75c6a18dff18d92560284d42cbaf50"},getCopy the code
Original link: blog.csdn.net/weixin_4793…