马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Vastsea 于 2020-5-13 13:25 编辑
如果采用这个代码,Excel的数值只有从最后一个IP获取的数值。import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ip = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.24']
for i in ip:
url = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
req = requests.get(url)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr.staticProp')[1].get_text()
count = trs
ws.title
ws['A1'] = "IP地址"
ws['B1'] = "总数"
for n in count:
ws.append([i,count])
wb.save(""c:\\New folder\\11.xlsx"")
改成如下代码,提示错误。import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ip = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.24']
for i in ip:
url = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
req = requests.get(url)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr.staticProp')[1].get_text()
n = n+1
n = 1
count = trs
ws.title
ws['A1'] = "IP地址"
ws['B1'] = "总数"
for n in ip:
ws.append(ws['A+n'],ws['B+n'])
wb.save("c:\\New folder\\11.xlsx")
报错如下:
TypeError Traceback (most recent call last)
<ipython-input-5-122806803084> in <module>
13 trs = bs.select('tr.staticProp')[1].get_text()
14 print(i,trs)
---> 15 n == n+1
16 n == 1
17 count = trs
TypeError: can only concatenate str (not "int") to str
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = "IP地址"
ws['B1'] = "总数"
n = 2
ip = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.24']
for i in ip:
url = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
req = requests.get(url)
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
wb.save("c:\\New folder\\11.xlsx")
|