-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_url.py
More file actions
38 lines (34 loc) · 1.12 KB
/
Copy pathget_url.py
File metadata and controls
38 lines (34 loc) · 1.12 KB
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
# encoding=utf-8
import json
import os
import random
import threading
import time
import traceback
import urllib2
import requests
import logging
class get_url:
def __init__(self, proxy_pool=None, session=None):
if session == None:
self.session = requests.Session()
else:
self.session = session
self.proxy_pool = proxy_pool
def get(self, url, timeout=5):
for i in range(10): # 用不同的代理最多尝试10次,获取到数据则直接返回
proxy = self.proxy_pool.get_proxy() # 从代理池中获取一个代理
try:
response = self.session.get(
url, proxies=proxy, timeout=timeout)
except Exception, e:
# traceback.print_exc()
self.proxy_pool.add_wrong(proxy)
logging.warning('invalid-proxy')
continue
try:
return response.text
except Exception, e:
logging.warning('get-response-text-exception')
traceback.print_exc()
raise Exception('my_exception', url)