hksp_search.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. @author: luojunhui
  3. 好看视频搜索爬虫
  4. """
  5. import requests
  6. import urllib.parse
  7. import time
  8. import hashlib
  9. from applications.functions.common import MySQLServer
  10. def get_video_detail(video_id):
  11. """
  12. 获取好看视频的视频链接
  13. :param video_id:
  14. :return:
  15. """
  16. url = "https://haokan.baidu.com/v"
  17. params = {
  18. 'vid': video_id,
  19. '_format': 'json',
  20. # 'hk_nonce': 'f47386e95fe657182aa3c1826d9a6b85',
  21. # 'hk_timestamp': '1715225386',
  22. # 'hk_sign': '4b219f5e3971e42b3e23dc2a209fc9d9',
  23. # 'hk_token': 'Dg8DdAVwdwNzDHcFcXF+D3gHBQA'
  24. }
  25. headers = {
  26. 'Accept': '*/*',
  27. 'cookie': "BIDUPSID='",
  28. 'Accept-Language': 'en,zh;q=0.9,zh-CN;q=0.8',
  29. 'Cache-Control': 'no-cache',
  30. 'Connection': 'keep-alive',
  31. 'Content-Type': 'application/x-www-form-urlencoded',
  32. 'Referer': 'https://haokan.baidu.com',
  33. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
  34. }
  35. response = requests.request("GET", url, headers=headers, params=params).json()
  36. return response['data']['apiData']['curVideoMeta']
  37. def hksp_search(key):
  38. """
  39. 好看视频搜索爬虫
  40. """
  41. sensitive_words = MySQLServer().select_sensitive_words()
  42. def sensitive_flag(s_words, ori_title):
  43. """
  44. :param ori_title:
  45. :param s_words:
  46. :return:
  47. """
  48. for word in s_words:
  49. if word in ori_title:
  50. return False
  51. return True
  52. timestamp_seconds = time.time()
  53. timestamp_milliseconds = int(timestamp_seconds * 1000)
  54. url = 'https://haokan.baidu.com/haokan/ui-search/pc/search/video'
  55. # 定义请求的参数
  56. strings = "{}_{}_{}_{}_{}".format(1, urllib.parse.quote(key), 10, timestamp_milliseconds, 1)
  57. sign = hashlib.md5(strings.encode()).hexdigest()
  58. params = {
  59. 'pn': 1,
  60. 'rn': 10,
  61. 'type': 'video',
  62. 'query': key,
  63. 'sign': sign,
  64. 'version': 1,
  65. 'timestamp': timestamp_milliseconds
  66. }
  67. # 定义请求头
  68. headers = {
  69. 'authority': 'haokan.baidu.com',
  70. 'accept': '*/*',
  71. 'accept-language': 'zh,en;q=0.9,zh-CN;q=0.8',
  72. 'cookie': "BIDUPSID=",
  73. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
  74. 'x-requested-with': 'xmlhttprequest',
  75. }
  76. # 发送GET请求
  77. response = requests.get(url, headers=headers, params=params).json()
  78. data_list = response['data']['list']
  79. L = []
  80. for data in data_list:
  81. try:
  82. video_id = data['vid']
  83. res = get_video_detail(video_id)
  84. if sensitive_flag(sensitive_words, ['title']) and int(res['duration']) <= 300:
  85. L.append(res)
  86. else:
  87. continue
  88. except:
  89. pass
  90. return L