crawler.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import json
  2. import re
  3. from .utils import get_page
  4. from pyquery import PyQuery as pq
  5. class ProxyMetaclass(type):
  6. def __new__(cls, name, bases, attrs):
  7. count = 0
  8. attrs['__CrawlFunc__'] = []
  9. for k, v in attrs.items():
  10. if 'crawl_' in k:
  11. attrs['__CrawlFunc__'].append(k)
  12. count += 1
  13. attrs['__CrawlFuncCount__'] = count
  14. return type.__new__(cls, name, bases, attrs)
  15. class Crawler(object, metaclass=ProxyMetaclass):
  16. def get_proxies(self, callback):
  17. proxies = []
  18. for proxy in eval("self.{}()".format(callback)):
  19. print('成功获取到代理', proxy)
  20. proxies.append(proxy)
  21. return proxies
  22. # def crawl_daxiang(self):
  23. # url = 'http://vtp.daxiangdaili.com/ip/?tid=559363191592228&num=50&filter=on'
  24. # html = get_page(url)
  25. # if html:
  26. # urls = html.split('\n')
  27. # for url in urls:
  28. # yield url
  29. def crawl_daili66(self, page_count=4):
  30. """
  31. 获取代理66
  32. :param page_count: 页码
  33. :return: 代理
  34. """
  35. start_url = 'http://www.66ip.cn/{}.html'
  36. urls = [start_url.format(page) for page in range(1, page_count + 1)]
  37. for url in urls:
  38. print('Crawling', url)
  39. html = get_page(url)
  40. if html:
  41. doc = pq(html)
  42. trs = doc('.containerbox table tr:gt(0)').items()
  43. for tr in trs:
  44. ip = tr.find('td:nth-child(1)').text()
  45. port = tr.find('td:nth-child(2)').text()
  46. yield ':'.join([ip, port])
  47. def crawl_proxy360(self):
  48. """
  49. 获取Proxy360
  50. :return: 代理
  51. """
  52. start_url = 'http://www.proxy360.cn/Region/China'
  53. print('Crawling', start_url)
  54. html = get_page(start_url)
  55. if html:
  56. doc = pq(html)
  57. lines = doc('div[name="list_proxy_ip"]').items()
  58. for line in lines:
  59. ip = line.find('.tbBottomLine:nth-child(1)').text()
  60. port = line.find('.tbBottomLine:nth-child(2)').text()
  61. yield ':'.join([ip, port])
  62. def crawl_goubanjia(self):
  63. """
  64. 获取Goubanjia
  65. :return: 代理
  66. """
  67. start_url = 'http://www.goubanjia.com/free/gngn/index.shtml'
  68. html = get_page(start_url)
  69. if html:
  70. doc = pq(html)
  71. tds = doc('td.ip').items()
  72. for td in tds:
  73. td.find('p').remove()
  74. yield td.text().replace(' ', '')
  75. def crawl_ip181(self):
  76. start_url = 'http://www.ip181.com/'
  77. html = get_page(start_url)
  78. ip_address = re.compile('<tr.*?>\s*<td>(.*?)</td>\s*<td>(.*?)</td>')
  79. # \s* 匹配空格,起到换行作用
  80. re_ip_address = ip_address.findall(html)
  81. for address,port in re_ip_address:
  82. result = address + ':' + port
  83. yield result.replace(' ', '')
  84. def crawl_ip3366(self):
  85. for page in range(1, 4):
  86. start_url = 'http://www.ip3366.net/free/?stype=1&page={}'.format(page)
  87. html = get_page(start_url)
  88. ip_address = re.compile('<tr>\s*<td>(.*?)</td>\s*<td>(.*?)</td>')
  89. # \s * 匹配空格,起到换行作用
  90. re_ip_address = ip_address.findall(html)
  91. for address, port in re_ip_address:
  92. result = address+':'+ port
  93. yield result.replace(' ', '')
  94. def crawl_kxdaili(self):
  95. for i in range(1, 11):
  96. start_url = 'http://www.kxdaili.com/ipList/{}.html#ip'.format(i)
  97. html = get_page(start_url)
  98. ip_address = re.compile('<tr.*?>\s*<td>(.*?)</td>\s*<td>(.*?)</td>')
  99. # \s* 匹配空格,起到换行作用
  100. re_ip_address = ip_address.findall(html)
  101. for address, port in re_ip_address:
  102. result = address + ':' + port
  103. yield result.replace(' ', '')
  104. def crawl_premproxy(self):
  105. for i in ['China-01','China-02','China-03','China-04','Taiwan-01']:
  106. start_url = 'https://premproxy.com/proxy-by-country/{}.htm'.format(i)
  107. html = get_page(start_url)
  108. if html:
  109. ip_address = re.compile('<td data-label="IP:port ">(.*?)</td>')
  110. re_ip_address = ip_address.findall(html)
  111. for address_port in re_ip_address:
  112. yield address_port.replace(' ','')
  113. def crawl_xroxy(self):
  114. for i in ['CN','TW']:
  115. start_url = 'http://www.xroxy.com/proxylist.php?country={}'.format(i)
  116. html = get_page(start_url)
  117. if html:
  118. ip_address1 = re.compile("title='View this Proxy details'>\s*(.*).*")
  119. re_ip_address1 = ip_address1.findall(html)
  120. ip_address2 = re.compile("title='Select proxies with port number .*'>(.*)</a>")
  121. re_ip_address2 = ip_address2.findall(html)
  122. for address,port in zip(re_ip_address1,re_ip_address2):
  123. address_port = address+':'+port
  124. yield address_port.replace(' ','')
  125. def crawl_kuaidaili(self):
  126. for i in range(1, 4):
  127. start_url = 'http://www.kuaidaili.com/free/inha/{}/'.format(i)
  128. html = get_page(start_url)
  129. if html:
  130. ip_address = re.compile('<td data-title="IP">(.*?)</td>')
  131. re_ip_address = ip_address.findall(html)
  132. port = re.compile('<td data-title="PORT">(.*?)</td>')
  133. re_port = port.findall(html)
  134. for address,port in zip(re_ip_address, re_port):
  135. address_port = address+':'+port
  136. yield address_port.replace(' ','')
  137. def crawl_xicidaili(self):
  138. for i in range(1, 3):
  139. start_url = 'http://www.xicidaili.com/nn/{}'.format(i)
  140. headers = {
  141. 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  142. 'Cookie':'_free_proxy_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFVEkiJWRjYzc5MmM1MTBiMDMzYTUzNTZjNzA4NjBhNWRjZjliBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMUp6S2tXT3g5a0FCT01ndzlmWWZqRVJNek1WanRuUDBCbTJUN21GMTBKd3M9BjsARg%3D%3D--2a69429cb2115c6a0cc9a86e0ebe2800c0d471b3',
  143. 'Host':'www.xicidaili.com',
  144. 'Referer':'http://www.xicidaili.com/nn/3',
  145. 'Upgrade-Insecure-Requests':'1',
  146. }
  147. html = get_page(start_url, options=headers)
  148. if html:
  149. find_trs = re.compile('<tr class.*?>(.*?)</tr>', re.S)
  150. trs = find_trs.findall(html)
  151. for tr in trs:
  152. find_ip = re.compile('<td>(\d+\.\d+\.\d+\.\d+)</td>')
  153. re_ip_address = find_ip.findall(tr)
  154. find_port = re.compile('<td>(\d+)</td>')
  155. re_port = find_port.findall(tr)
  156. for address,port in zip(re_ip_address, re_port):
  157. address_port = address+':'+port
  158. yield address_port.replace(' ','')
  159. def crawl_ip3366(self):
  160. for i in range(1, 4):
  161. start_url = 'http://www.ip3366.net/?stype=1&page={}'.format(i)
  162. html = get_page(start_url)
  163. if html:
  164. find_tr = re.compile('<tr>(.*?)</tr>', re.S)
  165. trs = find_tr.findall(html)
  166. for s in range(1, len(trs)):
  167. find_ip = re.compile('<td>(\d+\.\d+\.\d+\.\d+)</td>')
  168. re_ip_address = find_ip.findall(trs[s])
  169. find_port = re.compile('<td>(\d+)</td>')
  170. re_port = find_port.findall(trs[s])
  171. for address,port in zip(re_ip_address, re_port):
  172. address_port = address+':'+port
  173. yield address_port.replace(' ','')
  174. def crawl_iphai(self):
  175. start_url = 'http://www.iphai.com/'
  176. html = get_page(start_url)
  177. if html:
  178. find_tr = re.compile('<tr>(.*?)</tr>', re.S)
  179. trs = find_tr.findall(html)
  180. for s in range(1, len(trs)):
  181. find_ip = re.compile('<td>\s+(\d+\.\d+\.\d+\.\d+)\s+</td>', re.S)
  182. re_ip_address = find_ip.findall(trs[s])
  183. find_port = re.compile('<td>\s+(\d+)\s+</td>', re.S)
  184. re_port = find_port.findall(trs[s])
  185. for address,port in zip(re_ip_address, re_port):
  186. address_port = address+':'+port
  187. yield address_port.replace(' ','')
  188. def crawl_89ip(self):
  189. start_url = 'http://www.89ip.cn/apijk/?&tqsl=1000&sxa=&sxb=&tta=&ports=&ktip=&cf=1'
  190. html = get_page(start_url)
  191. if html:
  192. find_ips = re.compile('(\d+\.\d+\.\d+\.\d+:\d+)', re.S)
  193. ip_ports = find_ips.findall(html)
  194. for address_port in ip_ports:
  195. yield address_port
  196. def crawl_data5u(self):
  197. start_url = 'http://www.data5u.com/free/gngn/index.shtml'
  198. headers = {
  199. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  200. 'Accept-Encoding': 'gzip, deflate',
  201. 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
  202. 'Cache-Control': 'max-age=0',
  203. 'Connection': 'keep-alive',
  204. 'Cookie': 'JSESSIONID=47AA0C887112A2D83EE040405F837A86',
  205. 'Host': 'www.data5u.com',
  206. 'Referer': 'http://www.data5u.com/free/index.shtml',
  207. 'Upgrade-Insecure-Requests': '1',
  208. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
  209. }
  210. html = get_page(start_url, options=headers)
  211. if html:
  212. ip_address = re.compile('<span><li>(\d+\.\d+\.\d+\.\d+)</li>.*?<li class=\"port.*?>(\d+)</li>', re.S)
  213. re_ip_address = ip_address.findall(html)
  214. for address, port in re_ip_address:
  215. result = address + ':' + port
  216. yield result.replace(' ', '')