-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcollect_ips.py
44 lines (35 loc) · 1.33 KB
/
collect_ips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
from bs4 import BeautifulSoup
import re
import os
# 目标URL列表
urls = ['https://ip.164746.xyz/ipTop10.html',
'https://cf.090227.xyz'
]
# 正则表达式用于匹配IP地址
ip_pattern = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
# 检查ip.txt文件是否存在,如果存在则删除它
if os.path.exists('ip.txt'):
os.remove('ip.txt')
# 创建一个文件来存储IP地址
with open('ip.txt', 'w') as file:
for url in urls:
# 发送HTTP请求获取网页内容
response = requests.get(url)
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 根据网站的不同结构找到包含IP地址的元素
if url == 'https://ip.164746.xyz/ipTop10.html':
elements = soup.find_all('tr')
elif url == 'https://cf.090227.xyz':
elements = soup.find_all('tr')
else:
elements = soup.find_all('li')
# 遍历所有元素,查找IP地址
for element in elements:
element_text = element.get_text()
ip_matches = re.findall(ip_pattern, element_text)
# 如果找到IP地址,则写入文件
for ip in ip_matches:
file.write(ip + '\n')
print('IP地址已保存到ip.txt文件中。')