This repository has been archived by the owner on Jun 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
crawl.py
229 lines (226 loc) · 10.4 KB
/
crawl.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import requests
import chardet
from bs4 import BeautifulSoup
from database import DB
import re
import socket
import random
import time
from printer import *
def get_agent():
return random.choice(open("ua1.txt",encoding="utf-8").read().split("\n"))
def get_page(url):
try:
while True:
r=requests.get(url,headers={"User-Agent": get_agent()})
#assert r.status==200
if r.status_code==503:
error("503拒绝服务")
return None
assert r.ok,"状态码错误"
r.encoding=chardet.detect(bytes(r.content))["encoding"]
#return r.text.strip("\t").strip("\n")
return r.text.strip().strip("\n")
except Exception as e:
error("爬虫发生错误"+str(e))
#error(r.text)
return None
class ProxyMetaclass(type):
def __new__(cls,names,bases,attrs):
count=0
attrs["__CrawlFunc__"]=[]
for k,v in attrs.items():
if k.startswith("crawl_"):
attrs["__CrawlFunc__"].append(k)
attrs["__CrawlFunc__"].sort()
count+=1
attrs["__CrawlFuncCount__"]=count
return type.__new__(cls,names,bases,attrs)
class Crawler(object,metaclass=ProxyMetaclass):
def get_proxies(self,callback):
proxies=[]
try:
for proxy in eval("self.{}()".format(callback)):
ok("[获取器] 成功获取代理%s"%proxy)
proxies.append(proxy)
warning("[获取器] 共获取IP %s个"%len(proxies))
return proxies
except Exception as e:
error(str(e))
return []
def crawl_89ip(self):
url="http://www.89ip.cn/tqdl.html?api=1&num=1000&port=&address=&isp="
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
yield from re.findall(r"\d+\.\d+\.\d+\.\d+\:\d+",page)
def ncrawl_89ip(self): #已失效
url="http://www.89ip.cn/index_{}.html"
urls=[url.format(page) for page in range(1,7)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=[x.strip("<td>").strip("</td>") for x in re.findall("\d+\.\d+\.\d+\.\d+",page,re.S)]
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page,re.S)]
for x in zip(ip,port):
yield ":".join(x)
def crawl_xici(self):
urls2=["https://www.xicidaili.com/nn/{}","https://www.xicidaili.com/nt/{}","https://www.xicidaili.com/nt/{}","https://www.xicidaili.com/wt/{}","https://www.xicidaili.com/qq/{}"]
for urI in urls2:
urls=[urI.format(x) for x in range(1,201)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
ip=re.findall("\d+\.\d+\.\d+\.\d+",page)
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
time.sleep(2)
def crawl_kuaidaili(self):
urls2=["https://www.kuaidaili.com/free/inha/{}/","https://www.kuaidaili.com/free/intr/{}/"]
for url in urls2:
urls=[url.format(page) for page in range(1,50)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
bs=BeautifulSoup(page,"html.parser")
ip=[str(x).strip('<td data-title="IP">').strip("</td>") for x in bs.find_all("td",attrs={"data-title":"IP"})]
port=[str(x).strip('<td data-title="PORT">').strip("</td>") for x in bs.find_all("td",attrs={"data-title":"PORT"})]
for x in zip(ip,port):
yield ":".join(x)
time.sleep(3)
def crawl_yqie(self):
url="http://ip.yqie.com/ipproxy.htm"
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall("\d+\.\d+\.\d+\.\d+",page)
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
def crawl_xila(self):
url="http://www.xiladaili.com/putong/{}/"
urls=[url.format(page) for page in range(1,51)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall("\d+\.\d+\.\d+\.\d+:\d+",page)
yield from ip
time.sleep(3)
def crawl_nima(self):
urls2=["http://www.nimadaili.com/gaoni/{}","http://www.nimadaili.com/http/{}","http://www.nimadaili.com/https/{}"]
for urI in urls2:
urls=[urI.format(x) for x in range(1,51)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
yield from re.findall("\d+\.\d+\.\d+\.\d+\:\d+",page)
time.sleep(2)
def crawl_7yun(self):
url="https://www.7yip.cn/free/?action=china&page={}"
urls=[url.format(page) for page in range(1,51)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
bs=BeautifulSoup(page,"html.parser")
ip=[str(x).strip('<td data-title="IP" class="has-responsive-th"><span class="responsive-th">IP</span>').strip("</td>") for x in bs.find_all("td",attrs={"data-title":"IP"})]
port=[str(x).strip('<td data-title="IP" class="has-responsive-th"><span class="responsive-th">PORT</span>').strip("</td>") for x in bs.find_all("td",attrs={"data-title":"PORT"})]
for x in zip(ip,port):
yield ":".join(x)
def crawl_wndaili(self):
url="http://www.wndaili.cn/free/?stype=2&page={}"
urls=[url.format(x) for x in range(1,31)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+\.\d+\.\d+\.\d+</td>",page)]
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
def crawl_spfs(self):
url="http://www.superfastip.com/welcome/freeip/{}"
urls=[url.format(x) for x in range(1,11)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall(r"\d+\.\d+\.\d+\.\d+")
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
time.sleep(5)
def ncrawl_31f(self): #网站关闭
urls=["http://31f.cn/https-proxy/","http://31f.cn/http-proxy/"]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall(r"\d+\.\d+\.\d+\.\d+",page)
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
def crawl_3366(self):
urls2=["http://www.ip3366.net/free/?stype=1&page={}","http://www.ip3366.net/free/?stype=2&page={}"]
for urI in urls2:
urls=[urI.format(x) for x in range(1,51)]
for url in urls:
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall("\d+\.\d+\.\d+\.\d+",page)
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
time.sleep(5)
def crawl_iplist(self):
url="http://www.thebigproxylist.com/members/proxy-api.php?output=all&user=list&pass=8a544b2637e7a45d1536e34680e11adf"
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
yield from [x.split(",")[0] for x in page.split("\n")]
def crawl_proxyant(self):
url="http://www.feiyiproxy.com/?page_id=1457"
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall("\d+\.\d+\.\d+\.\d+",page)
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
def crawl_lassin(self):
url="https://lab.crossincode.com/proxy/"
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
ip=re.findall("\d+\.\d+\.\d+\.\d+",page)
port=[x.strip("<td>").strip("</td>") for x in re.findall("<td>\d+</td>",page)]
for x in zip(ip,port):
yield ":".join(x)
def crawl_syssuper(self):
url="http://ip.syssuper.com:8000/ip.php"
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
getip=lambda domain:socket.getaddrinfo(domain, 'http')[0][4][0]
if page:
ip=re.findall(r"\d+\.\d+\.\d+\.\d+",page)
coms=re.findall(r"\w+\.yunip168\.com",page)
ip+=[getip(x) for x in coms]
yield from [x+":80" for x in ip]
def crawl_daili66(self):
url="http://www.66ip.cn/mo.php?sxb=&tqsl=1000&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=http%3A%2F%2Fwww.66ip.cn%2F%3Fsxb%3D%26tqsl%3D1000%26ports%255B%255D2%3D%26ktip%3D%26sxa%3D%26radio%3Dradio"
printSkyBlue("[获取器] 正在爬取"+url)
page=get_page(url)
if page:
yield from re.findall(r"\d+\.\d+\.\d+\.\d+\:\d+",page)
def run(self):
db=DB()
warning("[获取器] 开始执行爬虫")
for call in self.__CrawlFunc__:
pr=self.get_proxies(call)
db.add(list(pr))