crawler.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_daxiang(self):
  22. """
  23. 获取大象代理
  24. :return: 代理
  25. """
  26. url = 'http://vtp.daxiangdaili.com/ip/?tid=555546364094534&num=100'
  27. html = get_page(url)
  28. if html:
  29. urls = html.split('\n')
  30. for url in urls:
  31. yield url
  32. def crawl_kuaidaili(self):
  33. """
  34. 获取快代理
  35. :return: 代理
  36. """
  37. 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'
  38. html = get_page(url)
  39. if html:
  40. result = json.loads(html)
  41. proxies = result.get('data').get('proxy_list')
  42. for proxy in proxies:
  43. yield proxy
  44. def crawl_daili66(self, page_count=4):
  45. """
  46. 获取代理66
  47. :param page_count: 页码
  48. :return: 代理
  49. """
  50. start_url = 'http://www.66ip.cn/{}.html'
  51. urls = [start_url.format(page) for page in range(1, page_count + 1)]
  52. for url in urls:
  53. print('Crawling', url)
  54. html = get_page(url)
  55. if html:
  56. doc = pq(html)
  57. trs = doc('.containerbox table tr:gt(0)').items()
  58. for tr in trs:
  59. ip = tr.find('td:nth-child(1)').text()
  60. port = tr.find('td:nth-child(2)').text()
  61. yield ':'.join([ip, port])
  62. def crawl_proxy360(self):
  63. """
  64. 获取Proxy360
  65. :return: 代理
  66. """
  67. start_url = 'http://www.proxy360.cn/Region/China'
  68. print('Crawling', start_url)
  69. html = get_page(start_url)
  70. if html:
  71. doc = pq(html)
  72. lines = doc('div[name="list_proxy_ip"]').items()
  73. for line in lines:
  74. ip = line.find('.tbBottomLine:nth-child(1)').text()
  75. port = line.find('.tbBottomLine:nth-child(2)').text()
  76. yield ':'.join([ip, port])
  77. def crawl_goubanjia(self):
  78. """
  79. 获取Goubanjia
  80. :return: 代理
  81. """
  82. start_url = 'http://www.goubanjia.com/free/gngn/index.shtml'
  83. html = get_page(start_url)
  84. if html:
  85. doc = pq(html)
  86. tds = doc('td.ip').items()
  87. for td in tds:
  88. td.find('p').remove()
  89. yield td.text().replace(' ', '')