Python web crawler. First Test-Youdao translator

Source: Internet
Author: User
Tags urlencode python web crawler

2018-03-0720:53:56

The effect of success is as follows

Code Backup

#-*-coding:utf-8-*- fromUrllibImportRequest fromUrllibImportParseImportJSONif __name__=="__main__":    #the corresponding request URLRequest_url ='HTTP://FANYI.YOUDAO.COM/TRANSLATE?SMARTRESULT=DICT&SMARTRESULT=RULE&SMARTRESULT=UGC'    #Create a Form_data dictionary that stores the form DataForm_data ={} form_data['I'] ='Hello'form_data[' from'] ='AUTO'form_data[' to'] ='AUTO'form_data['Smartresult'] ='Dict'form_data['Client'] ='Fanyideskweb'form_data['Salt'] ='1520425271933'form_data[' Sign'] ='ba35f5fdee5f0658938245b5f216d897'form_data['DOCTYPE'] ="JSON"form_data['version'] ='2.1'form_data['Keyfrom'] ='Fanyi.web'form_data['Action'] ='Fy_by_realtime'form_data['Typoresult'] ='false'    #using the UrlEncode method to convert a standard formatdata = Parse.urlencode (Form_data). Encode ('Utf-8')    #pass the Request object and the data in the finished formatResponse =Request.urlopen (request_url,data)Print(Response.getcode ())#read information and decodehtml = Response.read (). Decode ('Utf-8')    #using JSONTranslate_results =json.loads (HTML)#Find Translation ResultsTranslate_results = translate_results["Translateresult"][0][0]['TGT']    #Print translation Information    Print(HTML)Print("The result of the translation is:%s"% translate_results)

The effect is still possible, after all, it is the first time of its own debugging.

Code updates

How to add JSON data parsing

#-*-coding:utf-8-*- fromUrllibImportRequest fromUrllibImportParseImportJSONif __name__=="__main__":    #the corresponding request URLRequest_url ='HTTP://FANYI.YOUDAO.COM/TRANSLATE?SMARTRESULT=DICT&SMARTRESULT=RULE&SMARTRESULT=UGC'    #Create a Form_data dictionary that stores the form DataForm_data ={} form_data['I'] ='I used to have a dream'form_data[' from'] ='AUTO'form_data[' to'] ='AUTO'form_data['Smartresult'] ='Dict'form_data['Client'] ='Fanyideskweb'form_data['Salt'] ='1520425271933'form_data[' Sign'] ='ba35f5fdee5f0658938245b5f216d897'form_data['DOCTYPE'] ="JSON"form_data['version'] ='2.1'form_data['Keyfrom'] ='Fanyi.web'form_data['Action'] ='Fy_by_realtime'form_data['Typoresult'] ='false'    #using the UrlEncode method to convert a standard formatdata = Parse.urlencode (Form_data). Encode ('Utf-8')    #pass the Request object and the data in the finished formatResponse =Request.urlopen (request_url,data)#read information and decodehtml = Response.read (). Decode ('Utf-8')    #using JSONTranslate_results =json.loads (HTML)Print("output JSON data is:%s"%translate_results)#find the available key    Print("the available keys are:%s"%Translate_results.keys ())#Find Translation ResultsTest = translate_results["type"] Your_input= translate_results["Translateresult"][0][0]['src'] Translate_results= translate_results["Translateresult"][0][0]['TGT']    #Print translation Information    Print("Test Output%s"%test)Print("the content to be translated is:%s"%your_input)Print("The result of the translation is:%s"% translate_results)

The output result is

C:\Users\Administrator\PycharmProjects\python_test1\venv\Scripts\python.exe c:/users/administrator/ Pycharmprojects/python_test1/123the. PY output JSON data is: {'type':'Zh_cn2en','ErrorCode': 0,'ElapsedTime': 1,'Translateresult': [[{'src':'I used to have a dream','TGT':'I had a dream'}]]} is available with the key: Dict_keys (['type','ErrorCode','ElapsedTime','Translateresult']) test output zh_cn2en to be translated: I used to have a dream the result of translation is: I had a dreamprocess finished with exit code 0

Python web crawler. First Test-Youdao translator

Related Article

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.