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

 

posted @ 2018-04-16 15:30  NAVYSUMMER  阅读(1051)  评论(0编辑  收藏  举报
交流群 编程书籍