# # -*-coding:utf-8-*- import random import requests from fake_useragent import UserAgent """ 此百度翻译接口,释义会有不全的情况 """ def query(content): ip_pool = [{'http': '218.201.77.143:9091'}, {'http': '101.132.100.26:80'}, {'http': '47.92.113.71:80'}, {'http': '211.142.96.250:9091'}, {'http': '112.5.56.2:9091'}, {'http': '61.160.223.141:7302'}, {'http': '111.42.175.236:9091'}, {'http': '218.75.38.154:9091'}, {'http': '120.237.144.77:9091'}, {'http': '120.196.188.21:9091'}] # 调用 ua = UserAgent() headers = ua.random # 随机 header = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 'Connection': 'keep-alive', 'User-Agent': headers } proxies = random.choice(ip_pool) url = "https://fanyi.baidu.com/sug" content = str(content).replace(' ', '') data = { "kw": content } resp = requests.post(url, data=data, proxies=proxies, headers=header, timeout=10) # print(resp.content) data_list = resp.json()['data'] for item in data_list: print(f"{item['k']}: {item['v']}") if __name__ == '__main__': val = "salad" query(val)