zq_jieshu.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. # import re
  3. import datetime
  4. import re
  5. import scrapy
  6. from ..items import Zujieguo
  7. class HgjieshuSpider(scrapy.Spider):
  8. name = 'zq_jieshu'
  9. to_day = datetime.datetime.now()
  10. allowed_domains = ['hg3535z.com']
  11. custom_settings = {
  12. "ITEM_PIPELINES":{
  13. 'hg3535.pipelines.Zujieshuqiupipeline': 300,
  14. },
  15. 'LOG_LEVEL': 'DEBUG',
  16. 'LOG_FILE': "../hg3535/log/zq_jieshu_{}_{}_{}.log".format(to_day.year, to_day.month, to_day.day)
  17. }
  18. # start_urls = ['https://hg3535z.com/zh-cn/info-centre/sportsbook-info/results/1/normal/1', 'https://hg3535z.com/zh-cn/info-centre/sportsbook-info/results/1/normal/2']
  19. def start_requests(self):
  20. urls = ['https://hg3535z.com/zh-cn/info-centre/sportsbook-info/results/1/normal/1','https://hg3535z.com/zh-cn/info-centre/sportsbook-info/results/1/normal/2']
  21. for url in urls:
  22. yield scrapy.Request(url, callback=self.parse, dont_filter=True)
  23. def parse(self, response):
  24. if response.status == 200:
  25. # 所有比赛对象
  26. # tema = response.xpath('//div[@class="rt-event"]//span[@class="pt"]/text()')
  27. # print(tema)
  28. # 所有比赛队名
  29. # tema_name = [i.extract() for i in tema]
  30. # 获得所有比分对象
  31. # tema_score = response.xpath('//div[contains(@class,"rt-ft ")]')
  32. tema_score = response.xpath('//div[@class="flex-wrap"]/../div[5]')
  33. # print(tema_score)
  34. # 获得所有比赛id对象
  35. tema_id = response.xpath('//div[@class="flex-wrap"]/../div[1]/@id')
  36. # str.replace()
  37. # 所有比赛id列表
  38. temaid_list = [i.extract().replace('e-', "") for i in tema_id]
  39. temascore_list = []
  40. for score in tema_score:
  41. # 正则匹配规则
  42. p1 = r"\d{1,3}-\d{1,3}"
  43. pattern1 = re.compile(p1)
  44. try:
  45. # 获取正则匹配结果
  46. c = pattern1.findall(score.extract())[0]
  47. temascore_list.append(c)
  48. except:
  49. c = ""
  50. temascore_list.append(c)
  51. # print(temaid_list)
  52. # print(temascore_list)
  53. # print(len(temaid_list))
  54. # print(len(temascore_list))
  55. # 赛事id,赛事比元组列表
  56. tema_tupe = {(temaid_list[i], temascore_list[i]) for i in range(len(temaid_list))}
  57. print(tema_tupe)
  58. # print(tema_tupe)
  59. # print(len(tema_tupe))
  60. for y in tema_tupe:
  61. if y[1]:
  62. item = Zujieguo()
  63. item['id_score'] = y
  64. yield item