两个链接怎么For循环
如何下代码,原来只去一个链接的内容,现在增加了一个链接,该如何写?红色为增加的链接。import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = "IP地址"
ws['B1'] = "总数"
n = 2
ip1 = ['192.168.3.12','192.168.55.20','192.168.3.18','192.168.70.18','192.168.3.15','192.168.70.20','192.168.70.24']
for i in ip1:
url1 = "http://" + i + "/web/guest/cn/websys/status/getUnificationCounter.cgi"
url3 = "http://" + i + "/web/guest/cn/websys/status/configuration.cgi"
req = requests.get(url1)
html = req.text
bs = BeautifulSoup(html,"html.parser")
trs = bs.select('tr.staticProp').get_text()
r = str(n)
n = n + 1
ws['a' + r] = i
ws['b' + r] = i
ws['c' + r] = trs
wb.save("C:\\New folder\\11.xlsx") 本帖最后由 suchocolate 于 2021-8-12 08:11 编辑
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = "IP地址"
ws['B1'] = "总数"
n = 2
ips = ['192.168.3.12', '192.168.55.20', '192.168.3.18', '192.168.70.18', '192.168.3.15', '192.168.70.20',
'192.168.70.24']
urls = ["http://%s/web/guest/cn/websys/status/getUnificationCounter.cgi",
"http://%s/web/guest/cn/websys/status/configuration.cgi"]
for i in ips:
for url in urls:
req = requests.get(url % i)
html = req.text
bs = BeautifulSoup(html, "html.parser")
trs = bs.select('tr.staticProp').get_text()
r = str(n)
n = n + 1
ws['a' + r] = i
ws['b' + r] = i
ws['c' + r] = trs
wb.save("C:\\New folder\\11.xlsx")
suchocolate 发表于 2021-8-12 08:05
非常感谢。运行时给出如下错误。---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-5932eff03bb4> in <module>
6 for i in ip1:
7 for url in urls:
----> 8 req = requests.get(urls % i)
9 html = req.text
10 bs = BeautifulSoup(html,"html.parser")
TypeError: unsupported operand type(s) for %: 'list' and 'str' Vastsea 发表于 2021-8-12 10:50
非常感谢。运行时给出如下错误。
req = requests.get(url % i)
我写的是url,你写的urls,仔细核对。 本帖最后由 Vastsea 于 2021-8-12 11:51 编辑
suchocolate 发表于 2021-8-12 11:06
req = requests.get(url % i)
我写的是url,你写的urls,仔细核对。
对不住,想自己写,不想一粘了之。还是搞错了。
两个链接取的位置不同,该怎么处理?
第一个链接
bs.select('tr.staticProp').get_text()
第二个链接
bs.select('tr.staticProp').get_text() Vastsea 发表于 2021-8-12 11:36
对不住,想自己写,不想一粘了之。还是搞错了。
两个链接取的位置不同,该怎么处理?
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = "IP地址"
ws['B1'] = "总数"
n = 2
ips = ['192.168.3.12', '192.168.55.20', '192.168.3.18', '192.168.70.18', '192.168.3.15', '192.168.70.20',
'192.168.70.24']
urls = [f"http://%s/web/guest/cn/websys/status/getUnificationCounter.cgi",
f"http://%s/web/guest/cn/websys/status/configuration.cgi"]
for i in ips:
for x, url in enumerate(urls):
req = requests.get(url % i)
html = req.text
bs = BeautifulSoup(html, "html.parser")
if x:
trs = bs.select('tr.staticProp').get_text()
else:
trs = bs.select('tr.staticProp').get_text()
r = str(n)
n = n + 1
ws['a' + r] = i
ws['b' + r] = i
ws['c' + r] = trs
wb.save("C:\\New folder\\11.xlsx")
页:
[1]