码迷,mamicode.com
首页 > 编程语言 > 详细

python 爬取有道翻译

时间:2020-07-03 01:19:17      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:code   been   headers   action   除了   div   use   alt   image   

# 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 爬取有道翻译

标签:code   been   headers   action   除了   div   use   alt   image   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!