Baidu translated to write a python

Operating environment: python 3.6.0

The purpose of today in practice, it is written in python a Baidu translation is how to do it, in fact it is to get the interface to access through this interface, but the emergence of the middle point is really a problem, but to get rid of all

After the first dry drawing air Code

operation result:

 

Code:

# - * - Coding: UTF-8 - * -

"" "
Function: Baidu Translation
note: English automatically switch
" ""

Import Requests
Import Re


class Baidu_Translate (Object):
DEF __init __ (Self, the QUERY_STRING):
self.query_string the QUERY_STRING =
self.url_1 = 'https://fanyi.baidu.com/sug'
# self.url = 'https://fanyi.baidu.com/v2transapi' # can not use this address here, because the other side using the anti-reptile measures, visit this address is the people that will not give you any data
self.url_0 = 'https://fanyi.baidu.com/transapi'
self.zh_pattern = re.compile ( '[\ u4e00- \ u9fa5] +' )
self.headers = {
'the Accept': '* / *',
'the Accept - Encoding': 'the gzip, the deflate',
'the Accept - Language': 'the CN-ZH, ZH; Q = 0.9',
'Connection': 'keep - alive',
'The Content-the Type': 'file application / X-WWW-form-urlencoded; charset = UTF-. 8',
'the User-- Agent': 'the Mozilla / 5.0 (the Windows NT 10.0; the WOW64) AppleWebKit / 537.36 (KHTML, like the Gecko) the Chrome / 72.0.3626.121 Safari / 537.36 ',
' X-requested-With-':' the XMLHttpRequest ',
}

DEF get_post_data (Self):
"" "
to get the post request to upload parameter, and determines the type of input and be returned
: return: query words
"" "
IF re.search (pattern = self.zh_pattern, String = self.query_string): # input contains Chinese, for the Chinese it is determined that the input
return {
" from ":" zh ",
" to ": "EN",
"kw": self.query_string, # url_1 keyword fuzzy query
"query": self.query_string, # url_0 precise query keyword
}
the else:
return {
"from": "en",
"to": "zh",
"kw": self.query_string, # url_1 fuzzy search keyword
"query": self.query_string, # accurate keyword query url_0
}

DEF request_translate (Self):
"" "
requests to Baidu json data
: return: the request to Baidu json data
"" "
data = self.get_post_data ()
the try:
response_0 = requests.request (Method =" POST ", URL = self.url_0, headers = self.headers, data = data) .json ()
the except Exception: # any requested data exception handling
response_0 = ''
the try:
response_1 = requests.request (Method = "POST", URL = self.url_1, headers = self.headers, data = data) .json ()
the except exception: for # any exception handling data requests
response_1 = ''
return response_0,response_1

def parse_translate_data(self):
"""
Data analysis, the content analyzing request and translated into output
: return: None
"" "
response_0 = self.request_translate () [0]
response_1 = self.request_translate () [. 1]
# = response_0 Item
IF response_0:
Item = response_0. GET ( 'Data') [0] .get ( 'DST')
Print ( 'Key Word:', self.query_string, '\ T', 'Translate:', Item)
IF response_1:
Data = response_1.get ( ' Data ')
Print ()
for Item Data in [:. 1]: length is generally # 5, here only retain its Interpretation
print (' key word: \ t [{key}] 'format (key = item.get (.' K ')))
Print (' value: \ T \ T [{value}] 'the format (item.get value = (.' V ')))
Print ()
# Print (response_1.get (' Data '))


main DEF ():
"" "
The main function
: return: None
"" "
True the while:
the try:
query_keywords the INPUT = ( "" "Please enter the content you want to translate [Enter four '0' Exit]:" "")
IF query_keywords == "0000": # If you enter four '0' exit applet
print ( '########## You have successfully quit baidu translation ##########')
BREAK
the else:
baidu = Baidu_Translate (the QUERY_STRING = query_keywords)
baidu.parse_translate_data ()
Exception AS E the except:
Print ( 'request error, please try again', e.args)


IF the __name__ == '__main__':
main ()
------------------- -

Guess you like

Origin www.cnblogs.com/ly570/p/11007567.html