使用Python批量查询ip地址的信息
import requests
from openpyxl import load_workbook
from bs4 import BeautifulSoup
wb = load_workbook(r'ip.xlsx')
ips = []
def load_ips(wb):
dst_ips = wb['Sheet1']['A']
for i,ip in enumerate(dst_ips):
ips.append(ip.value)
def check(ips):
url = 'https://x.threatbook.com/v5/ip/'
headers = {
'Cookie' : '实际Cookie信息'
}
result_sheet = wb.create_sheet('result')
for i,ip in enumerate(ips):
res = requests.get(url + ip, headers = headers)
soup = BeautifulSoup(res.text, 'html.parser')
dstip = ip
level = soup.find('div', class_ = 'judgments-result').text.strip()
city = soup.find('span', class_ = 'location').text.strip()
commons = soup.find_all('div', class_ = "vb-tag-common")
tags = []
for common in commons:
tag = common.text.strip()
tags.append(tag)
result_sheet[f"A{i+1}"].value = dstip
result_sheet[f"B{i+1}"].value = level
result_sheet[f"C{i+1}"].value = city
result_sheet[f"D{i+1}"].value = ','.join(tags)
print('success')
if __name__ == '__main__':
load_ips(wb)
check(ips)
wb.save('ip.xlsx')