-
Notifications
You must be signed in to change notification settings - Fork 71
/
IP89Fetcher.py
49 lines (42 loc) · 1.87 KB
/
IP89Fetcher.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
45
46
47
48
49
# encoding: utf-8
from .BaseFetcher import BaseFetcher
import requests
from pyquery import PyQuery as pq
import re
class IP89Fetcher(BaseFetcher):
"""
https://www.89ip.cn/
"""
def fetch(self):
"""
执行一次爬取,返回一个数组,每个元素是(protocol, ip, port),portocal是协议名称,目前主要为http
返回示例:[('http', '127.0.0.1', 8080), ('http', '127.0.0.1', 1234)]
"""
urls = []
for page in range(1, 6):
url = f'https://www.89ip.cn/index_{page}.html'
urls.append(url)
proxies = []
ip_regex = re.compile(r'^\d+\.\d+\.\d+\.\d+$')
port_regex = re.compile(r'^\d+$')
for url in urls:
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/79.0.3945.130 Chrome/79.0.3945.130 Safari/537.36'
}
html = requests.get(url, headers=headers, timeout=10).text
doc = pq(html)
for line in doc('tr').items():
tds = list(line('td').items())
if len(tds) == 5:
ip = tds[0].text().strip()
port = tds[1].text().strip()
if re.match(ip_regex, ip) is not None and re.match(port_regex, port) is not None:
proxies.append(('http', ip, int(port)))
return list(set(proxies))