python通过token登录,并爬取数据实例

 1 from bs4 import BeautifulSoup
 2 import requests
 3 
 4 class Zabbix(object):
 5     def __init__(self, headers):
 6         self.session = requests.Session()
 7         self.headers = headers
 8     def func(self):
 9         headers={
10             'Host':'172.28.14.165',
11             'Upgrade-Insecure-Requests':'1',
12             'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
13             'cookie':'zbx_sessionid='+token
14         }
15         response=self.session.get(url='http://172.28.14.165/zabbix/zabbix.php?action=report.status&ddreset=1',headers=headers,allow_redirects=False)
16         sourse = BeautifulSoup(response.text, 'html.parser')
17         text=sourse.find('span', {'class': 'green'})
18         if not text :
19             text=sourse.find('span', {'class': 'red'})
20         tq=text.string
21         print(tq)
22 if __name__ == '__main__':
23     headers = {
24         'Host': '172.28.14.165',
25         'Origin': 'http://172.28.14.165',
26         'Referer':'http://172.28.14.165/zabbix/index.php',
27         'Upgrade-Insecure-Requests':'1',
28         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36',
29     }
30     status = Zabbix(headers=headers)
31     token=input("please input token:")
32     status.func()
View Code

 

转载于:https://www.cnblogs.com/navysummer/p/8856115.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值