crawler.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import json
  2. from .utils import get_page
  3. from pyquery import PyQuery as pq
  4. class ProxyMetaclass(type):
  5. def __new__(cls, name, bases, attrs):
  6. count = 0
  7. attrs['__CrawlFunc__'] = []
  8. for k, v in attrs.items():
  9. if 'crawl_' in k:
  10. attrs['__CrawlFunc__'].append(k)
  11. count += 1
  12. attrs['__CrawlFuncCount__'] = count
  13. return type.__new__(cls, name, bases, attrs)
  14. class Crawler(object, metaclass=ProxyMetaclass):
  15. def get_proxies(self, callback):
  16. proxies = []
  17. for proxy in eval("self.{}()".format(callback)):
  18. print('成功获取到代理', proxy)
  19. proxies.append(proxy)
  20. return proxies
  21. def crawl_xdaili(self):
  22. """
  23. 获取讯代理
  24. :return: 代理
  25. """
  26. url = 'http://www.xdaili.cn/ipagent/greatRecharge/getGreatIp?spiderId=da289b78fec24f19b392e04106253f2a&orderno=YZ20177140586mTTnd7&returnType=2&count=20'
  27. html = get_page(url)
  28. if html:
  29. result = json.loads(html)
  30. proxies = result.get('RESULT')
  31. for proxy in proxies:
  32. yield proxy.get('ip') + ':' + proxy.get('port')
  33. def crawl_kuaidaili(self):
  34. """
  35. 获取快代理
  36. :return: 代理
  37. """
  38. url = 'http://dev.kuaidaili.com/api/getproxy/?orderid=959961765125099&num=100&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=1&an_an=1&an_ha=1&quality=1&format=json&sep=2'
  39. html = get_page(url)
  40. if html:
  41. result = json.loads(html)
  42. proxies = result.get('data').get('proxy_list')
  43. for proxy in proxies:
  44. yield proxy
  45. def crawl_daili66(self, page_count=4):
  46. """
  47. 获取代理66
  48. :param page_count: 页码
  49. :return: 代理
  50. """
  51. start_url = 'http://www.66ip.cn/{}.html'
  52. urls = [start_url.format(page) for page in range(1, page_count + 1)]
  53. for url in urls:
  54. print('Crawling', url)
  55. html = get_page(url)
  56. if html:
  57. doc = pq(html)
  58. trs = doc('.containerbox table tr:gt(0)').items()
  59. for tr in trs:
  60. ip = tr.find('td:nth-child(1)').text()
  61. port = tr.find('td:nth-child(2)').text()
  62. yield ':'.join([ip, port])
  63. def crawl_proxy360(self):
  64. """
  65. 获取Proxy360
  66. :return: 代理
  67. """
  68. start_url = 'http://www.proxy360.cn/Region/China'
  69. print('Crawling', start_url)
  70. html = get_page(start_url)
  71. if html:
  72. doc = pq(html)
  73. lines = doc('div[name="list_proxy_ip"]').items()
  74. for line in lines:
  75. ip = line.find('.tbBottomLine:nth-child(1)').text()
  76. port = line.find('.tbBottomLine:nth-child(2)').text()
  77. yield ':'.join([ip, port])
  78. def crawl_goubanjia(self):
  79. """
  80. 获取Goubanjia
  81. :return: 代理
  82. """
  83. start_url = 'http://www.goubanjia.com/free/gngn/index.shtml'
  84. html = get_page(start_url)
  85. if html:
  86. doc = pq(html)
  87. tds = doc('td.ip').items()
  88. for td in tds:
  89. td.find('p').remove()
  90. yield td.text().replace(' ', '')