hg3535_zq_status.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import jsonpath
  4. import scrapy
  5. import time
  6. from scrapy.http import Request
  7. import psycopg2
  8. import time
  9. from functools import wraps
  10. from contextlib import contextmanager
  11. import psycopg2.extras
  12. from ..items import Zuqiustatus
  13. import json
  14. from datetime import datetime
  15. from datetime import date
  16. import itertools
  17. # 测试一个函数的运行时间,使用方式:在待测函数直接添加此修饰器
  18. def timethis(func):
  19. @wraps(func)
  20. def wrapper(*args, **kwargs):
  21. start = time.perf_counter()
  22. r = func(*args, **kwargs)
  23. end = time.perf_counter()
  24. print('\n============================================================')
  25. print('{}.{} : {}'.format(func.__module__, func.__name__, end - start))
  26. print('============================================================\n')
  27. return r
  28. return wrapper
  29. # 测试一段代码运行的时间,使用方式:上下文管理器with
  30. # with timeblock('block_name'):
  31. # your_code_block...
  32. @contextmanager
  33. def timeblock(label='Code'):
  34. start = time.perf_counter()
  35. try:
  36. yield
  37. finally:
  38. end = time.perf_counter()
  39. print('==============================================================')
  40. print('{} run time: {}'.format(label, end - start))
  41. print('==============================================================')
  42. class SqlConn():
  43. '''
  44. 连接数据库,以及进行一些操作的封装
  45. '''
  46. sql_name = ''
  47. database = ''
  48. user = ''
  49. password = ''
  50. port = 0
  51. host = ''
  52. # 创建连接、游标
  53. def __init__(self, *args, **kwargs):
  54. if kwargs.get("sql_name"):
  55. self.sql_name = kwargs.get("sql_name")
  56. if kwargs.get("database"):
  57. self.database = kwargs.get("database")
  58. if kwargs.get("user"):
  59. self.user = kwargs.get("user")
  60. if kwargs.get("password"):
  61. self.password = kwargs.get("password")
  62. if kwargs.get("port"):
  63. self.port = kwargs.get("port")
  64. if kwargs.get("host"):
  65. self.host = kwargs.get("host")
  66. if not (self.host and self.port and self.user and
  67. self.password and self.database):
  68. raise Warning("conn_error, missing some params!")
  69. sql_conn = {
  70. 'postgresql': psycopg2,
  71. }
  72. self.conn = sql_conn[self.sql_name].connect(host=self.host,
  73. port=self.port,
  74. user=self.user,
  75. password=self.password,
  76. database=self.database,
  77. )
  78. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  79. if not self.cursor:
  80. raise Warning("conn_error!")
  81. # 测试连接
  82. def test_conn(self):
  83. if self.cursor:
  84. print("conn success!")
  85. else:
  86. print('conn error!')
  87. # 单条语句的并提交
  88. def execute(self, sql_code):
  89. self.cursor.execute(sql_code)
  90. self.conn.commit()
  91. # 单条语句的不提交
  92. def execute_no_conmmit(self, sql_code):
  93. self.cursor.execute(sql_code)
  94. # 构造多条语句,使用%s参数化,对于每个list都进行替代构造
  95. def excute_many(self, sql_base, param_list):
  96. self.cursor.executemany(sql_base, param_list)
  97. # 批量执行(待完善)
  98. def batch_execute(self, sql_code):
  99. pass
  100. # 获取数据
  101. def get_data(self, sql_code, count=0):
  102. self.cursor.execute(sql_code)
  103. if int(count):
  104. return self.cursor.fetchmany(count)
  105. else:
  106. return self.cursor.fetchall()
  107. # 更新数据
  108. def updata_data(self, sql_code):
  109. self.cursor(sql_code)
  110. # 插入数据
  111. def insert_data(self, sql_code):
  112. self.cursor(sql_code)
  113. # 滚动游标
  114. def cursor_scroll(self, count, mode='relative'):
  115. self.cursor.scroll(count, mode=mode)
  116. # 提交
  117. def commit(self):
  118. self.conn.commit()
  119. # 回滚
  120. def rollback(self):
  121. self.conn.rollback()
  122. # 关闭连接
  123. def close_conn(self):
  124. self.cursor.close()
  125. self.conn.close()
  126. class ComplexEncoder(json.JSONEncoder):
  127. def default(self, obj):
  128. if isinstance(obj, datetime):
  129. return obj.strftime('%Y-%m-%d %H:%M:%S')
  130. elif isinstance(obj, date):
  131. return obj.strftime('%Y-%m-%d')
  132. else:
  133. return json.JSONEncoder.default(self, obj)
  134. class LanqiuSpider(scrapy.Spider):
  135. name = "ball_status"
  136. allowed_domains = ['hg3535z.com']
  137. #sid要改为1 足球 现在测试改为4
  138. start_urls = ['https://hg3535z.com/odds2/d/getodds?sid=1&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0'] # 滚球菜单 篮球滚球列url
  139. custom_settings = {
  140. "ITEM_PIPELINES": {
  141. 'hg3535.pipelines.BallStatuspipeline':200,
  142. }
  143. }
  144. # start_urls = ['http://hg3535z.com/odds2/d/getodds?sid=2&pt=3&ubt=am&pn=0&sb=2&dc=null&pid=0']
  145. # http: // hg3535z.com / odds2 / d / getamodds?eid = 3098030 & iip = false & ubt = am & isp = false
  146. # http://hg3535z.com/odds2/d/getodds?sid=2&pt=2&ubt=am&pn=0&sb=2&dc=null&pid=0
  147. def parse(self, response):
  148. datas = json.loads(response.text)
  149. ids = jsonpath.jsonpath(datas, '$..i-ot[0]..egs..es..i[16]') # ids新列表
  150. item = Zuqiustatus()
  151. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  152. # zuqiu_total = {}
  153. zuqiu_status_list = []
  154. if ids:
  155. ids = set(ids)
  156. for i in ids:
  157. zuqiu = {}
  158. zuqiu['match_id'] = i
  159. zuqiu['create_time'] = utime
  160. zuqiu['status'] = 1
  161. zuqiu['ball_type'] = datas['i-ot'][0]['s']['n']
  162. # item['match_id'] = i
  163. # item['create_time'] = utime
  164. # item['status'] = 1
  165. # item['ball_type'] = datas['i-ot'][0]['s']['n']
  166. # item['zuqiu_toal'] = zuqiu
  167. zuqiu_status_list.append(zuqiu)
  168. item["zuqiu_total"] = zuqiu_status_list
  169. yield item
  170. # urls = ['http://hg3535z.com/odds2/d/getodds?sid=2&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0''http://hg3535z.com/odds2/d/getodds?sid=3&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0','http://hg3535z.com/odds2/d/getodds?sid=4&pt=4&ubt=am&pn=0&sb=2&dc=null&pid=0']
  171. # for url in urls:
  172. # yield Request(url=url, callback=self.parse)