店播爬取Python脚本

persistent_mysql.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/python3
  2. #coding=utf-8
  3. #-*- coding: UTF-8 -*
  4. import pymysql
  5. import json
  6. class PersistentMysql:
  7. def __init__(self):
  8. host = 'rm-2ze8r493yk3636g3p.mysql.rds.aliyuncs.com'
  9. user = 'api_online'
  10. password = 'vetted8#hatbox'
  11. port = 3306
  12. # host = 'cdb-41ibb8ki.bj.tencentcdb.com'
  13. # user = 'dynamic'
  14. # password = 'J8tIG4$%1CGkgs*M'
  15. # port = 10043
  16. self.db = pymysql.connect(
  17. host = host,
  18. user = user,
  19. password = password,
  20. database = 'kuaishou',
  21. port = port,
  22. charset='utf8mb4'
  23. )
  24. # 使用cursor()方法获取操作游标
  25. self.cursor = self.db.cursor()
  26. def batch_insert(self, batch_data):
  27. insert_data = []
  28. # 收到弹幕
  29. if batch_data.get('user'):
  30. for data in batch_data.get('user'):
  31. # live_stream_id,type,principal_id,user_name,content,gift_id,batch_size,combo_count,rank,created_at
  32. action = (
  33. self.live_stream_id,
  34. 1,
  35. data.get('user').get('principalId'),
  36. json.loads(json.dumps(data.get('user').get('userName'))),
  37. json.loads(json.dumps(data.get('content'))),
  38. None,
  39. None,
  40. None,
  41. None,
  42. self.created_at
  43. )
  44. insert_data.append(action)
  45. # 收到礼物
  46. if batch_data.get('gift'):
  47. # live_stream_id,type,principal_id,user_name,content,gift_id,batch_size,combo_count,rank,created_at
  48. for data in batch_data.get('gift'):
  49. action = (
  50. self.live_stream_id,
  51. 2,
  52. data.get('user').get('principalId'),
  53. json.loads(json.dumps(data.get('user').get('userName'))),
  54. None,
  55. data.get('giftId'),
  56. data.get('batchSize'),
  57. data.get('comboCount'),
  58. data.get('rank'),
  59. self.created_at
  60. )
  61. insert_data.append(action)
  62. # 收到点亮
  63. if batch_data.get('like'):
  64. for data in batch_data.get('like'):
  65. action = (
  66. self.live_stream_id,
  67. 3,
  68. data.get('user').get('principalId'),
  69. json.loads(json.dumps(data.get('user').get('userName'))),
  70. None,
  71. None,
  72. None,
  73. None,
  74. None,
  75. self.created_at
  76. )
  77. insert_data.append(action)
  78. data_len = len(insert_data)
  79. if data_len == 0:
  80. # 关闭数据库连接
  81. self.db.close()
  82. return
  83. sql = 'insert into sc_kwai_barrage(live_stream_id,type,principal_id,user_name,content,gift_id,batch_size,combo_count,rank,created_at) value(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
  84. try:
  85. # 批量插入数据
  86. result = self.cursor.executemany(sql, insert_data)
  87. print(self.live_stream_id + '_捕获' + str(data_len) + '条数据,成功记录' + str(result) + '条数据')
  88. finally:
  89. # 提交到数据库执行
  90. self.db.commit()
  91. # 最终关闭数据库连接
  92. self.db.close()