Python爬虫实现简单翻译

import requests
import json
import os
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = {
    "from":"AUTO",
    "to":"AUTO",
    "smartresult":"dict",
    "client":"fanyideskweb",
    "salt":"15801391750396",
    "sign":"74bbb50b1bd6c62fbff24be5f3787e2f",
    "ts":"1580139175039",
    "bv":"e2a78ed30c66e16a857c5b6486a1d326",
    "doctype":"json",
    "version":"2.1",
    "keyfrom":"fanyi.web",
    "action":"FY_BY_CLICKBUTTION",
}

while 1:
    data['i']=input('Type in:\n') # 输入中文
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}
    response = requests.post(url=url,data=data,headers=head)
    name = response.json()
    print(name['translateResult'][0][0]['tgt'])
    c = input('Clear?(Y/N) or exit(exit/quit/leave):\n')
    if c == 'exit' or c == 'quit' or c == 'leave' or c == 'l' or c == 'e' or c == 'q':
        break
    if c == 'Y' or c == 'y' or c == 1 or c == '1':
        os.system('cls')
    else :
        print('\n')

注:记得安装 pip 中的正则表达式包 requests

posted @ 2023-08-20 20:17  Happy-Pig-Orz  阅读(4)  评论(0编辑  收藏  举报