Advertisement
lexquarkie

athletic_2905

May 29th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. # athletic parser
  2.  
  3. import time
  4. from grab.spider import Spider, Task, Data
  5. from weblib.logs import default_logging
  6. import re
  7. import sys
  8. import os
  9.  
  10. import config
  11.  
  12.  
  13.  
  14. def check_loaded(html='', slogan=config.SLOGAN):
  15.     """
  16.   Функция для проверки корректности загрузки страницы.
  17.   Проверяется посредством присутствия последовательности slogan в странице.
  18.   """
  19.     is_ok = slogan in str(html)
  20.     return is_ok
  21.  
  22. class LbCrawler(Spider):
  23.     initial_urls = config.INITIAL_URLS
  24.     base_url = config.INITIAL_URLS[0]
  25.     parsed_url = []
  26.  
  27.  
  28.     def task_initial(self, grab, task):
  29.        
  30.         if not check_loaded(grab.response.unicode_body()):
  31.             print('Can\'t start parsing on', task.url)
  32.  
  33.         for category_url in grab.doc.select('//*[@class="group"]/a').attr_list('href'):
  34.             yield Task('category', url=grab.make_url_absolute(str(category_url)), priority=95)
  35.        
  36.  
  37.     def task_category(self, grab, task):
  38.        
  39.         if not check_loaded(grab.response.unicode_body()):
  40.             yield task.clone(refresh_cache=True, priority=70)
  41.             return
  42.  
  43.         for product_url in grab.doc.select('//*[@class="image"]/a').attr_list('href'):        
  44.             print('Trying to parsing:  ' + product_url)
  45.             print('Category:           ' +  task.url)
  46.             time.sleep(.03)
  47.             yield Task('product',url=grab.make_url_absolute(str(product_url)), priority=95)
  48.  
  49.            
  50.  
  51.            
  52.  
  53.     def task_product(self, grab, task):
  54.         if not check_loaded(grab.response.body):
  55.             time.sleep(.05)
  56.             yield task.clone(refresh_cache=True, priority=60)
  57.  
  58.             return
  59.         print('Parsing product ... ' + task.url)
  60.         for product_table in grab.doc.select('//div[@class="browse-ttl"]/a').attr_list('href'):
  61.              yield Task('level_2', url=grab.make_url_absolute(str(url_level_2)), priority=85)
  62.  
  63.  
  64.  
  65. def start_parsing():
  66.     default_logging(grab_log=config.GRAB_LOG, network_log=config.NETWORK_LOG)
  67.     bot = LbCrawler(thread_number=config.THREAD_NUMBER)
  68.    
  69.     bot.cache_enabled = False
  70.     bot.cache = None
  71.     bot.charset = 'utf-8'
  72.    
  73.     # bot.load_proxylist(config.PROXY_LIST, 'text_file', proxy_type='http')  пока без проксей
  74.     bot.proxylist_enabled = False
  75.     bot.proxy = None
  76.  
  77.     try:
  78.         bot.run()
  79.     except KeyboardInterrupt:
  80.         pass
  81.     # if config.DEBUG:
  82.     #     bot.save_list('fatal', config.FATAL_ERROR_DUMP)
  83.     # comp_db.session.commit()
  84.     print(bot.render_stats())
  85.     sys.exit()
  86.  
  87. if __name__ == '__main__':
  88.     start_parsing()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement