""" 好看视频搜索爬虫 """ import json import requests import urllib.parse import time import hashlib def hksp_search(key): """ 好看视频搜索爬虫 """ timestamp_seconds = time.time() timestamp_milliseconds = int(timestamp_seconds * 1000) url = 'https://haokan.baidu.com/haokan/ui-search/pc/search/video' # 定义请求的参数 strings = "{}_{}_{}_{}_{}".format(1, urllib.parse.quote(key), 10, timestamp_milliseconds, 1) sign = hashlib.md5(strings.encode()).hexdigest() params = { 'pn': 1, 'rn': 10, 'type': 'video', 'query': key, 'sign': sign, 'version': 1, 'timestamp': timestamp_milliseconds } # 定义请求头 headers = { 'authority': 'haokan.baidu.com', 'accept': '*/*', 'accept-language': 'zh,en;q=0.9,zh-CN;q=0.8', 'cookie': "BIDUPSID='", # 'referer': 'https://haokan.baidu.com/web/search/page?query=%E8%80%81%E4%BA%BA', 'sec-ch-ua': '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', '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', 'x-requested-with': 'xmlhttprequest', } # 发送GET请求 response = requests.get(url, headers=headers, params=params).json() print(json.dumps(response, ensure_ascii=False, indent=4)) if __name__ == '__main__': hksp_search("人类首次从恐龙蛋化石中获得恐龙的遗传物质")