Vastsea 发表于 2020-5-14 09:44:09

两组不同IP如何使用判断语句?

我有两组IP对应不同的URL。

例如下面这组IP对应的是红色部分链接

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').get_text()
    r = str(n)
    n = n + 1
    ws['a' + r] = i
    ws['b' + r] = trs
wb.save("c:\\New folder\\11.xlsx")

下面这组IP对应的是红色部分链接

ip = ['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 i in ip:
    url = "http://" + i + "/SSI/info_configuration.htm"
    req = requests.get(url)
    html = req.text
    bs = BeautifulSoup(html,"html.parser")
    trs = bs.select('tr').get_text()

如何通过判断语句来执行这两组语句?

_2_ 发表于 2020-5-18 09:34:51

将两个 url 分别命名为 url1 和 url2
两个 ip 也这样 ip1 和 ip2
如果你想两个同时运行,可以尝试多线程

Vastsea 发表于 2020-5-18 20:45:47

本帖最后由 Vastsea 于 2020-5-18 20:46 编辑

ip1 = ['10.0.3.12','10.0.55.20','10.0.3.18','10.0.70.18','10.0.3.15','10.0.70.24']
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 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').get_text()
    n = 2
    r = str(n)
    n = n + 1
    ws['a' + r] = i
    ws['b' + r] = trs
wb.save("c:\\New folder\\11.xlsx")

for i in ip2:
    url2 = "http://" + i + "/SSI/info_configuration.htm"
    req = requests.get(url2)
    html = req.text
    bs = BeautifulSoup(html,"html.parser")
    trs = bs.select('tr').get_text()
    n = 8
    r = str(n)
    n = n + 1
    ws['a' + r] = i
    ws['b' + r] = trs
wb.save("c:\\New folder\\11.xlsx")

这样可以吗?
页: [1]
查看完整版本: 两组不同IP如何使用判断语句?