python的requests模块实现登录后的接口操作
#encoding :utf-8
import requests
import re
'定义登录url和传参data数据,并以post的方式登录系统,然后将生产的cookie用于接下来的接口查询使用,如果不使用cookie,则会提示用户未登录的字样'
login_url='http://xxxxxx:7001/pronline/Msg'
login_date={'FunName':'proauthDsAuth','username':'xxx','password':'xxx'}
res=requests.post(login_url,login_date)
cookies =res.cookies
# print res.text
ap_url = 'http://xxxxxx:7001/pronline/Msg?FunName@proauthWebAplist&devflag@2&_dc=1529416759321&groupid=&did=&keyword=&flags=1&sysmark=&servicecode=&page=1&start=0&limit=200&sort=dev_name&dir=DESC'
res_2=requests.get(ap_url,cookies=cookies) #用get方法获取具体的数据
result= res_2.text
#print result
#用正则表达式获取要获取的数据,数据示例如下:
[{'dev_name': '项城市丛碧国际酒店','dev_mac': '00117F121586', 'dev_mode': '','dev_hw': '','dev_sn': 'BCSH-AP10-2000-1700'},
{'dev_name': '为强客房','dev_mac': '00117F121664', 'dev_mode': '','dev_hw': '','dev_sn': 'BCSH-AP20-1605-0170'}]
res_3=re.findall(r"('dev_mac': '\S*'){1},",result)
print res_3
count = 0
for i in res_3:
print i
count += 1
print count
结果示例如下:
'dev_mac': 'F86CE1E74110'
'dev_mac': 'F86CE134F960'
'dev_mac': 'F86CE134F410'
'dev_mac': 'F86CE134F860'
'dev_mac': 'F86CE1E7B2E0'
'dev_mac': 'F86CE1E98B00'