近期公司需要开发一个能够读取设备内部网页信息,我想到了用爬虫的方法来获取相关数据,具体代码如下:
#!/usr/bin/env python3#-*- coding: UTF-8 -*-
importrequestsimporttimeclassGetData():#初始化变量
def __init__(self, url):
self.url=url#要用帐号密码登录,首先需要在先通过浏览器登录网页,然后在浏览器中查看Cookie值,将其放在请求头中
self.headers ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0","Cookie": "user_choose=block"}
self.login()#首先先用Session方法来登录网页
self.get_data() #登录完成后,我们就可以去登录里面其它的网址了
#登录主页,将Cookie写入session对象中
deflogin(self):
self.session=requests.Session()
response= self.session.get(self.url, headers=self.headers)#用session对象的post来获取Ajax接口数据
defget_data(self):
get_url= "http://192.168.1.12/boafrm/formCpReportMonitor"resp= self.session.post(get_url, headers&#