xicidaili.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from pyquery import PyQuery as pq
  2. from proxypool.schemas.proxy import Proxy
  3. from proxypool.crawlers.base import BaseCrawler
  4. from loguru import logger
  5. BASE_URL = 'https://www.xicidaili.com/'
  6. class XicidailiCrawler(BaseCrawler):
  7. """
  8. xididaili crawler, https://www.xicidaili.com/
  9. """
  10. urls = [BASE_URL]
  11. ignore = True
  12. headers = {
  13. 'User-Agent': 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
  14. }
  15. @logger.catch
  16. def crawl(self):
  17. """
  18. crawl main method
  19. """
  20. for url in self.urls:
  21. logger.info(f'fetching {url}')
  22. html = self.fetch(url, headers=self.headers)
  23. for proxy in self.parse(html):
  24. logger.info(f'fetched proxy {proxy.string()} from {url}')
  25. yield proxy
  26. def parse(self, html):
  27. """
  28. parse html file to get proxies
  29. :return:
  30. """
  31. doc = pq(html)
  32. items = doc('#ip_list tr:contains(高匿)').items()
  33. for item in items:
  34. country = item.find('td.country').text()
  35. if not country or country.strip() != '高匿':
  36. continue
  37. host = item.find('td:nth-child(2)').text()
  38. port = int(item.find('td:nth-child(3)').text())
  39. yield Proxy(host=host, port=port)
  40. if __name__ == '__main__':
  41. crawler = XicidailiCrawler()
  42. for proxy in crawler.crawl():
  43. print(proxy)