一、安装
1、cmd进入命令行界面
2、直接输入 D:切换至D盘(python所在路径),
然后cd python下的scripts所在路径,切换至pip所在位置
3、pip install requests 即可安装requests
其中,pip版本过低时,使用:python -m pipinstall –upgrade pip升级pip版本
二、requests的使用
循环访问一个网页多次:
import requests
url=’http://10.65.1.31:8080/snapshot_api?api_key=SMC_INT_DET07&resId=111/testlnnzymc_004-120180510091539&applicationKey=08f0550c20bf01603205e0d303e0600aa0aa02d0cf0e00aa’
fout = open(‘result.txt’,’w’)
sum = 0
x=1
while x < 5:
r=requests.get(url)
fout.write(url+’ : OK with status_code:’+str(r.status_code)+’\n’)
print(str(r.status_code))
sum = sum + x
x+=1
fout.close()
或者:
import requests
url = ‘http://www.baidu.com.cn/’
fout = open(‘result.txt’, ‘w’)
for i in range(10):
r=requests.post(url)
fout.write(url+’ : OK withstatus_code: ‘+str(r.status_code))
print(url+’ : OK withstatus_code: ‘+str(r.status_code))
fout.close()
实现一分钟之内访问一个连接超过100次,并查看结果:
import requests
url = ‘http://192.168.1.20:12306/I1/interfaceApi/info?resId=40000/zh_003_2018013021275120180130092753&applicationKey=0d605d0770110f40c70f908a0b40b40a50470e10680d50f6’
fout = open(‘result.txt’, ‘w’)
for i in range(102):
r=requests.get(url)
fout.write(str(i+1)+’ : OK with status_code: ‘+str(r.text)+’\n’)
print(str(i+1)+’ : OK with status_code: ‘+str(r.text)+’\n’)
fout.close()