我的python代码仅从循环中查找最后一个值

问题描述 投票:0回答:1

我有一个名为IP2.txt的文件,该文件包含2行,如下所示。

103.201.150.209
113.170.129.113

我的代码像这样,它读取文件IP2并查找网站搜索

import requests

from bs4 import BeautifulSoup

import re

fh = open('IP2.txt')

for line in fh:

        ip = line.rstrip()

        print(ip)

        loads = {'q':line.rstrip(),'engine':1}

        r = requests.get('https://fortiguard.com/search',params=loads)

#       print(r.url)

#       print(r.text)

Link_text = r.text

soup = BeautifulSoup(Link_text, 'lxml')

for product in soup.find_all('section', class_='iprep'):

    product_title = product.find("a").text

print(ip+':'+ product_title)

fh.close()

上面代码的输出是这样的。

103.201.150.209

113.170.129.113

113.170.129.113:Malicious Websites

您可以看到其阅读的最后一行,并跳过第一个值:103.201.150.209

请对此提供帮助非常感谢

python
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.