马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Vastsea 于 2020-5-20 12:08 编辑
第一次写代码,感谢大家的帮助,我觉得还有很多需要优化的地方。请大家指教一下。谢谢!
背景:我们租用了十几台打印机每月都要抄打印机的印数。因为是网络打印机又都有WEB管理界面,所以想到通过Python自动抄录的想法。
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = "IP地址"
ws['B1'] = "总数"
n = 2
ip1 = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.20','10.0.70.24','10.0.60.70']
for i in ip1:
url1 = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
req = requests.get(url1)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr.staticProp')[1].get_text()
r = str(n)
n = n + 1
ws['a' + r] = i
ws['b' + r] = trs[3:]
wb.save("C:\\New folder\\11.xlsx")
n = 10
ip2 = ['10.0.3.30','10.0.3.31','10.0.3.32','10.0.65.18','10.0.70.22','10.0.65.11','10.0.45.11']
for j in ip2:
url2 = "http://" + j + "/SSI/info_configuration.htm"
req = requests.get(url2)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr')[20].get_text()
r = str(n)
n = n + 1
ws['a' + r] = j
ws['b' + r] = trs.strip()[5:]
wb.save("C:\\New folder\\11.xlsx")
|