|
|
@@ -3,32 +3,30 @@
|
|
|
"""
|
|
|
-------------------------------------------------
|
|
|
File Name: GetFreeProxy.py
|
|
|
- Description : 通过关键字扫描censys.io中的疑似ip
|
|
|
+ Description : 抓取免费代理
|
|
|
Author : JHao
|
|
|
date: 2016/11/25
|
|
|
-------------------------------------------------
|
|
|
Change Activity:
|
|
|
- 2017/06/15: 通过关键字扫描censys.io中的疑似ip
|
|
|
+ 2016/11/25:
|
|
|
-------------------------------------------------
|
|
|
"""
|
|
|
-
|
|
|
-from lxml import etree
|
|
|
+import re
|
|
|
import requests
|
|
|
-import threading
|
|
|
|
|
|
-API_URL = "https://www.censys.io/ipv4/_search?q={k}&page={p}"
|
|
|
-header = {
|
|
|
- 'Host': 'www.censys.io',
|
|
|
- 'Connection': 'keep-alive',
|
|
|
- 'Accept': '*/*',
|
|
|
- 'X-Requested-With': 'XMLHttpRequest',
|
|
|
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
|
|
|
- 'Referer': 'https://www.censys.io/',
|
|
|
- 'Accept-Encoding': 'gzip, deflate, sdch, br',
|
|
|
- 'Accept-Language': 'zh-CN,zh;q=0.8'
|
|
|
-}
|
|
|
+try:
|
|
|
+ from importlib import reload # py3 实际不会实用,只是为了不显示语法错误
|
|
|
+except:
|
|
|
+ import sys # py2
|
|
|
+
|
|
|
+ reload(sys)
|
|
|
+ sys.setdefaultencoding('utf-8')
|
|
|
+
|
|
|
+from Util.utilFunction import robustCrawl, getHtmlTree
|
|
|
+from Util.WebRequest import WebRequest
|
|
|
|
|
|
-KEY_WORD = ['Squid', 'CCProxy', 'Tinyproxy', 'Wingate', 'Pound', 'Proxy', 'Mikrotik']
|
|
|
+# for debug to disable insecureWarning
|
|
|
+requests.packages.urllib3.disable_warnings()
|
|
|
|
|
|
|
|
|
class GetFreeProxy(object):
|
|
|
@@ -39,55 +37,98 @@ class GetFreeProxy(object):
|
|
|
def __init__(self):
|
|
|
pass
|
|
|
|
|
|
- def scanner_ip(self):
|
|
|
+ @staticmethod
|
|
|
+ @robustCrawl # decoration print error if exception happen
|
|
|
+ def freeProxyFirst(page=10):
|
|
|
+ """
|
|
|
+ 抓取无忧代理 http://www.data5u.com/
|
|
|
+ :param page: 页数
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ url_list = ['http://www.data5u.com/',
|
|
|
+ 'http://www.data5u.com/free/',
|
|
|
+ 'http://www.data5u.com/free/gngn/index.shtml',
|
|
|
+ 'http://www.data5u.com/free/gnpt/index.shtml']
|
|
|
+ for url in url_list:
|
|
|
+ html_tree = getHtmlTree(url)
|
|
|
+ ul_list = html_tree.xpath('//ul[@class="l2"]')
|
|
|
+ for ul in ul_list:
|
|
|
+ yield ':'.join(ul.xpath('.//li/text()')[0:2])
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ @robustCrawl
|
|
|
+ def freeProxySecond(proxy_number=100):
|
|
|
"""
|
|
|
- 根据关键字搜索ip
|
|
|
+ 抓取代理66 http://www.66ip.cn/
|
|
|
+ :param proxy_number: 代理数量
|
|
|
:return:
|
|
|
"""
|
|
|
- for key in KEY_WORD:
|
|
|
- for page in range(1, 200):
|
|
|
- url = API_URL.format(k=key, p=page)
|
|
|
- try:
|
|
|
- res = requests.get(url, headers=header, timeout=30, )
|
|
|
- if res.status_code != 200:
|
|
|
- break
|
|
|
- tree = etree.HTML(res.content)
|
|
|
- ip_list_el = tree.xpath('//span[@class="ip"]/a/text()')
|
|
|
- ip_list = [each.strip() for each in ip_list_el if each.strip()]
|
|
|
- for each in ip_list:
|
|
|
- yield each
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
- print('Key {k} page: {p}'.format(k=key, p=page))
|
|
|
-
|
|
|
-
|
|
|
-class ThreadScanner(threading.Thread):
|
|
|
- def __init__(self, key):
|
|
|
- super(ThreadScanner, self).__init__()
|
|
|
- self.key = key
|
|
|
- self.query = {'query': self.key, 'page': 1, 'fields': ['ip']}
|
|
|
-
|
|
|
- def run(self):
|
|
|
- self.scanner_ip()
|
|
|
-
|
|
|
- def scanner_ip(self):
|
|
|
- for page in range(1, 200):
|
|
|
- url = API_URL.format(k=self.key, p=page)
|
|
|
- try:
|
|
|
- res = requests.get(url, headers=header, timeout=30, proxies={'https': 'https://106.75.87.49:53100'})
|
|
|
- if res.status_code == 429:
|
|
|
- break
|
|
|
- tree = etree.HTML(res.content)
|
|
|
- ip_list_el = tree.xpath('//span[@class="ip"]/a/text()')
|
|
|
- ip_list = [each.strip() for each in ip_list_el if each.strip()]
|
|
|
- for each in ip_list:
|
|
|
- print(each)
|
|
|
- except Exception as e:
|
|
|
- print(e)
|
|
|
- print('Key {k} page: {p}'.format(k=self.key, p=page))
|
|
|
+ url = "http://www.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
|
|
|
+ proxy_number)
|
|
|
+ request = WebRequest()
|
|
|
+ html = request.get(url).content
|
|
|
+ for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
|
|
|
+ yield proxy
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ @robustCrawl
|
|
|
+ def freeProxyThird(days=1):
|
|
|
+ """
|
|
|
+ 抓取ip181 http://www.ip181.com/
|
|
|
+ :param days:
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ url = 'http://www.ip181.com/'
|
|
|
+ html_tree = getHtmlTree(url)
|
|
|
+ tr_list = html_tree.xpath('//tr')[1:]
|
|
|
+ for tr in tr_list:
|
|
|
+ yield ':'.join(tr.xpath('./td/text()')[0:2])
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ @robustCrawl
|
|
|
+ def freeProxyFourth():
|
|
|
+ """
|
|
|
+ 抓取西刺代理 http://api.xicidaili.com/free2016.txt
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ url_list = ['http://www.xicidaili.com/nn', # 高匿
|
|
|
+ 'http://www.xicidaili.com/nt', # 透明
|
|
|
+ ]
|
|
|
+ for each_url in url_list:
|
|
|
+ tree = getHtmlTree(each_url)
|
|
|
+ proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
|
|
|
+ for proxy in proxy_list:
|
|
|
+ yield ':'.join(proxy.xpath('./td/text()')[0:2])
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ @robustCrawl
|
|
|
+ def freeProxyFifth():
|
|
|
+ """
|
|
|
+ 抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ url = "http://www.goubanjia.com/free/gngn/index{page}.shtml"
|
|
|
+ for page in range(1, 10):
|
|
|
+ page_url = url.format(page=page)
|
|
|
+ tree = getHtmlTree(page_url)
|
|
|
+ proxy_list = tree.xpath('//td[@class="ip"]')
|
|
|
+ for each_proxy in proxy_list:
|
|
|
+ yield ''.join(each_proxy.xpath('.//text()'))
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- g = GetFreeProxy()
|
|
|
- for each in g.scanner_ip():
|
|
|
- print(each)
|
|
|
+ gg = GetFreeProxy()
|
|
|
+ # for e in gg.freeProxyFirst():
|
|
|
+ # print e
|
|
|
+
|
|
|
+ # for e in gg.freeProxySecond():
|
|
|
+ # print e
|
|
|
+
|
|
|
+ # for e in gg.freeProxyThird():
|
|
|
+ # print e
|
|
|
+
|
|
|
+ for e in gg.freeProxyFourth():
|
|
|
+ print e
|
|
|
+
|
|
|
+ # for e in gg.freeProxyFifth():
|
|
|
+ # print(e)
|