Getting started with python web crawler (2) -- using python to call Google Translate

Source: Internet
Author: User
Tags python web crawler

Getting started with python web crawler (2) -- using python to call Google Translate

I have been reading documents outside China recently. I don't know some new words. Google Translate is used for understanding, and F12 is used to view the source code on the next page. It is found that Google Translate's page function can be implemented simply using python. So I first searched online for similar articles and blogs and found a few good articles. So I simply implemented the translation function by referring to other code and my own ideas. The Code is as follows:

Import re import urllib, urllib2 # ---------- simulate browser behavior, send data to Google Translate, and then capture the Translation results. This is the general idea ------- def Gtranslate (text ): # text input to translate the English sentence Gtext = text # hl: browser, operating system language, the default is zh-CN # ie: the default is UTF-8 # text: is the string to be translated # langpair: Language pair, that is, 'en' | 'zh-cn' indicates that from English to simplified Chinese values = {'hl ': 'zh-cn ', 'ie': 'utf-8', 'text': Gtext, 'langpair ': "'en' | 'zh-cn'"} # URL used to store Google Translate url = 'HTTP: // translate.google.cn/'# Use urllib to upload the data in values. urlencode is escaped to a URL-specific format and then assigned to data storage. Data storage = urllib. urlencode (values) # generate a request req = urllib2.Request (URL, data) with url and data # disguise access by an IE6.0 browser, google will return a 403 error browser = 'mozilla/4.0 (Windows; U; MSIE 6.0; Windows NT 6.1; SV1 ;. net clr 2.0.50727) 'req. add_header ('user-agent', browser) # send the request response = urllib2.urlopen (req) to Google Translate # Read the returned page, then we can extract the translated string from the HTML page to html = response. read () # use a regular expression to match <= TRANSLATED_TEXT = ). The translated text is the content after the equal sign (p = re. compile (r "(? <= TRANSLATED_TEXT = ).*?; ") M = p. search (html) chineseText = m. group (0 ). strip (';') return chineseText if _ name _ = "_ main _": # Gtext is the string to be translated, Gtext = 'you shoshould believe yourself, you are the best one! And we sure that you will do something making us being proud of you 'print ('the input text: % s' % Gtext) chineseText = Gtranslate (Gtext ). strip ("'") print ('translated End, The output text: % s' % chineseText)
After running in IDLEz:



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.