hksp_search.py 3.0 KB

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