Python Learning Note: "Crawler + Youdao Dictionary" to achieve a simple English-Chinese translation Program

Source: Internet
Author: User

1. Youdao's translation

Pages: www.youdao.com

Fig1

Fig2

Fig3

Fig4

Click "Automatic Translation", select ' Network ' and select ' first ', as follows:

Fig5

Then the following is shown, and the red-framed part is where the code needs to be written:

Fig6

Fig7

Then look at the results of the translation:

Fig8

2.python realization of English-Chinese translation:

Principle: the need to translate the content into Youdao dictionary, and then through the program to climb down the translation results.

1 #-*-coding:utf-8-*-2 """3 Created on Sun 09:36:124 5 @author: 90Zeng6 """7 8 ImportUrllib9 ImportJSONTen  One #Note that this is encoded in Unicode, otherwise it will show garbled AContent = input (u"Please enter the content you want to translate:") - #URL is the Response URL in Fig6 -URL ='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc& sessionfrom=http://www.youdao.com/' the #the data format for crawling down is form data in Fig7 -data = {} -data['type'] ='AUTO' -data['I'] =content +data['DOCTYPE'] ='JSON' -data['xmlversion'] ='1.6' +data['Keyfrom'] ='Fanyi.web' Adata['UE'] ='UTF-8' atdata['Typoresult'] ='true' -  - #Data Encoding -data =urllib.urlencode (data) -  - #crawling content from URLs in the format of data inResponse =urllib.urlopen (URL, data) - #reads the crawled content into the variable string html, toHTML =Response.read () + #converts a string into a dictionary form as shown in Fig8 -target =json.loads (HTML) the #based on the FIG8 format, the final translation results are taken out *result = target["Translateresult"][0][0]['TGT'] $ Panax Notoginseng #Here Unicode display Chinese, avoid garbled - Print(U"translation Result:%s"% (target["Translateresult"][0][0]['TGT']))

Run:

Learning Source: The Little Turtle's video ' 0 basics of getting Started Python '

Python Learning Note: "Crawler + Youdao Dictionary" to achieve a simple English-Chinese translation Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.