Skip to content

Commit cf9ef54

Browse files
committed
添加代理,防止爬虫被办
1 parent 39e2967 commit cf9ef54

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

.gitignore

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ ENV/
9494
*.DS_Store
9595

9696
# custom dir
97-
Plugins
98-
other
99-
plugin
100-
log
97+
Plugins/
98+
other/
99+
plugin/
100+
log/

UnityAssetStore/middlewares.py

+11-22
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,16 @@
88
class ProxyMiddleware(object):
99
def process_request(self, request, spider):
1010
try:
11-
contents = requests.get('http://127.0.0.1:8000')
11+
r = requests.get('http://101.200.55.192:8000/?type=1&count=20')
1212
#logging.info('contents:%s' % contents.text)
13-
data = json.loads(contents.text)
14-
proxy = random.choice(data)
15-
ip = proxy.get('ip')
16-
port = proxy.get('port')
17-
address = str(ip) + ':' + str(port)
18-
#request.meta['proxy'] = "http://%s" % proxy['ip_port']
19-
request.meta['proxy'] = 'http://%s' % address
20-
logging.info('********ProxyMiddleware proxy*******:%s' % request.meta['proxy'] )
13+
data = json.loads(r.text)
14+
if len(data) > 0:
15+
proxy = random.choice(data)
16+
ip = proxy.get('ip')
17+
port = proxy.get('port')
18+
address = '%s:%s' % (ip, port)
19+
#request.meta['proxy'] = "http://%s" % proxy['ip_port']
20+
request.meta['proxy'] = 'http://%s' % address
21+
logging.info('********ProxyMiddleware proxy*******:%s' % request.meta['proxy'] )
2122
except:
22-
pass
23-
24-
25-
#
26-
#
27-
# PROXIES = [
28-
# {'ip_port': '111.11.228.75:80', 'user_pass': ''},
29-
# {'ip_port': '120.198.243.22:80', 'user_pass': ''},
30-
# {'ip_port': '111.8.60.9:8123', 'user_pass': ''},
31-
# {'ip_port': '101.71.27.120:80', 'user_pass': ''},
32-
# {'ip_port': '122.96.59.104:80', 'user_pass': ''},
33-
# {'ip_port': '122.224.249.122:8088', 'user_pass': ''},
34-
# ]
23+
pass

UnityAssetStore/settings.py

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 1020,
5959
'UnityAssetStore.middlewares.ProxyMiddleware': 100,
6060
}
61-
6261
# Enable or disable extensions
6362
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
6463
#EXTENSIONS = {

0 commit comments

Comments
 (0)