crawler.py 10 KB

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