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  删掉,便能请求成功。

原文地址:https://www.cnblogs.com/zijidefengge/p/13227973.html