python 爬取有道翻译

Posted 看星星的派大星

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 爬取有道翻译相关的知识,希望对你有一定的参考价值。

# translate words through youdao.com // discription about the code 
# the problem is the form data of youdao webpage has been coded

# import necessary package
import urllib.request
import urllib.parse
import json

def translate(translate_name):
    url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" # 这里删除了_0
    header = {} # build the headers especially for User-Agent
    header["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" # input the User-Agent
    data = { # built the data form
    \'i\':translate_name,
    \'from\':    "AUTO",
    \'to\':"AUTO",
    \'smartresult\':"dict",
    \'client\':"fanyideskweb",
    "salt":"15937031552563",
    \'sign\':"085de10feee0ff2cccd9743f523d97ed",
    \'ts\':"1593703155256",
    "bv":"e2a78ed30c66e16a857c5b6486a1d326",
    "doctype":"json",
    "version":"2.1",
    "keyfrom":"fanyi.web",
    "action":"FY_BY_REALTlME"
    }
    my_encode = urllib.parse.urlencode(data).encode("utf-8")
    my_response = urllib.request.Request(url=url, data=my_encode, headers=header) # build the request object
    result = urllib.request.urlopen(my_response).read().decode("utf-8") # parse the result
    my_json_result = json.loads(result)
    print(my_json_result["translateResult"][0][0]["tgt"])

if __name__=="__main__":
    while(True):
        my_translate_input = input("please input you translate word(press ! to quite):")
        if my_translate_input == \'!\':
            break
        else:
            translate(my_translate_input)

 

 

之前爬取没成功,这次成功了,时间为2020,7,2

 

最关键的是,把 url 里面的 _o  删掉,便能请求成功。

 

以上是关于python 爬取有道翻译的主要内容,如果未能解决你的问题,请参考以下文章

python爬取有道翻译

Python 爬虫篇 - 调用有道翻译api接口翻译外文网站的整篇西班牙文实战演示。爬取西班牙语文章调用有道翻译接口进行整篇翻译

一次有趣的爬虫经历

有道词典翻译(携带请求头和post参数请求)

python百度翻译爬虫

获取有道翻译页面