jieshu.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # encoding: utf-8
  2. import datetime
  3. import json
  4. import logging
  5. import redis
  6. import scrapy
  7. from ..items import Hgjieshu
  8. from ..settings import R_HOST, R_PASSWORD, R_POST, R_DB
  9. class HgjieshuSpider(scrapy.Spider):
  10. name = 'jieshu'
  11. to_day = datetime.datetime.now()
  12. allowed_domains = ['hg3535z.com']
  13. custom_settings = {
  14. "ITEM_PIPELINES": {
  15. 'hg3535.pipeline.jieshu.Jieshuqiupipeline': 300,
  16. },
  17. # 'LOG_LEVEL': 'DEBUG',
  18. # 'LOG_FILE': "../hg3535/log/saiguo{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day)
  19. }
  20. rls = redis.Redis(host=R_HOST, port=R_POST, db=R_DB, password=R_PASSWORD)
  21. def start_requests(self):
  22. match_ids = self.rls.hgetall("hg3535.GunQiu.ids")
  23. if match_ids:
  24. for match_id, ctime in match_ids.items():
  25. match_id, ctime = match_id.decode(), ctime.decode()
  26. url = 'https://odata.yonghuai5515.com/odds6i/d/getamodds/zh-cn/eid/{}/iip/true/ubt/am/isp/false'.format(match_id)
  27. yield scrapy.Request(url=url, callback=self.parse, dont_filter=True, meta={'ctime': ctime})
  28. def parse(self, response):
  29. logger = logging.getLogger(__name__)
  30. ctime = response.meta['ctime']
  31. try:
  32. data = json.loads(response.text)
  33. status = data['i'][0]
  34. # if not status:
  35. ball = data['i'][1]
  36. match_id = data['i'][2]
  37. home = data['eg']['es'][0]['i'][0]
  38. away = data['eg']['es'][0]['i'][1]
  39. date = ctime
  40. item = Hgjieshu()
  41. item['ball'] = ball
  42. item['match_id'] = match_id
  43. item['status'] = status
  44. item['home'] = home
  45. item['away'] = away
  46. item['date'] = date
  47. yield item
  48. except Exception as e:
  49. logger.warning(e)
  50. return