baidu_fanyi.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # # -*-coding:utf-8-*-
  2. import random
  3. import requests
  4. from fake_useragent import UserAgent
  5. """
  6. 此百度翻译接口,释义会有不全的情况
  7. """
  8. def query(content):
  9. ip_pool = [{'http': '218.201.77.143:9091'}, {'http': '101.132.100.26:80'}, {'http': '47.92.113.71:80'},
  10. {'http': '211.142.96.250:9091'}, {'http': '112.5.56.2:9091'}, {'http': '61.160.223.141:7302'},
  11. {'http': '111.42.175.236:9091'}, {'http': '218.75.38.154:9091'}, {'http': '120.237.144.77:9091'},
  12. {'http': '120.196.188.21:9091'}]
  13. # 调用
  14. ua = UserAgent()
  15. headers = ua.random # 随机
  16. header = {
  17. "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",
  18. 'Connection': 'keep-alive',
  19. 'User-Agent': headers
  20. }
  21. proxies = random.choice(ip_pool)
  22. url = "https://fanyi.baidu.com/sug"
  23. content = str(content).replace(' ', '')
  24. data = {
  25. "kw": content
  26. }
  27. resp = requests.post(url, data=data, proxies=proxies, headers=header, timeout=10)
  28. # print(resp.content)
  29. data_list = resp.json()['data']
  30. for item in data_list:
  31. print(f"{item['k']}: {item['v']}")
  32. if __name__ == '__main__':
  33. val = "salad"
  34. query(val)