基于python實(shí)現(xiàn)百度翻譯功能
運(yùn)行環(huán)境: python 3.6.0
今天處于練習(xí)的目的,就用 python 寫了一個(gè)百度翻譯,是如何做到的呢,其實(shí)呢就是拿到接口,通過這個(gè)接口去訪問,不過中間確實(shí)是出現(xiàn)了點(diǎn)問題,不過都解決掉了
先晾圖后晾代碼
運(yùn)行結(jié)果:
代碼:
# -*- coding: utf-8 -*- """ 功能:百度翻譯 注意事項(xiàng):中英文自動(dòng)切換 """ import requests import re class Baidu_Translate(object): def __init__(self, query_string): self.query_string = query_string self.url_1 = 'https://fanyi.baidu.com/sug' # self.url = 'https://fanyi.baidu.com/v2transapi' # 這里不能用這個(gè)地址,因?yàn)閷?duì)方采用了反爬蟲措施,訪問這個(gè)地址是人家是不會(huì)給你任何數(shù)據(jù)的 self.url_0 = 'https://fanyi.baidu.com/transapi' self.zh_pattern = re.compile('[\u4e00-\u9fa5]+') self.headers = { 'Accept': '* / *', 'Accept - Encoding': 'gzip, deflate', 'Accept - Language': 'zh-CN, zh; q=0.9', 'Connection': 'keep - alive', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest', } def get_post_data(self): """ 拿到 post 請(qǐng)求上傳的參數(shù),并判斷輸入類型并予以返回 :return: 查詢?cè)~ """ if re.search(pattern=self.zh_pattern, string=self.query_string): # 輸入的內(nèi)容含有中文,則判別其為中文輸入 return { "from": "zh", "to": "en", "kw": self.query_string, # 模糊查詢 url_1關(guān)鍵詞 "query": self.query_string, # 精準(zhǔn)查詢 url_0關(guān)鍵詞 } else: return { "from": "en", "to": "zh", "kw": self.query_string, # 模糊查詢 url_1關(guān)鍵詞 "query": self.query_string, # 精準(zhǔn)查詢 url_0關(guān)鍵詞 } def request_translate(self): """ 向百度請(qǐng)求 json 數(shù)據(jù) :return: 向百度請(qǐng)求的 json 數(shù)據(jù) """ data = self.get_post_data() try: response_0 = requests.request(method="post", url=self.url_0, headers=self.headers, data=data).json() except Exception: # 進(jìn)行數(shù)據(jù)請(qǐng)求的任何異常處理 response_0 = '' try: response_1 = requests.request(method="post", url=self.url_1, headers=self.headers, data=data).json() except Exception: # 進(jìn)行數(shù)據(jù)請(qǐng)求的任何異常處理 response_1 = '' return response_0, response_1 def parse_translate_data(self): """ 數(shù)據(jù)解析,將請(qǐng)求到的翻譯內(nèi)容解析并輸出 :return: None """ response_0 = self.request_translate()[0] response_1 = self.request_translate()[1] # item = response_0 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 in data[:1]: # 長(zhǎng)度一般為5,這里只保留其釋義 print('key word: \t[ {key} ]'.format(key=item.get('k'))) print('value: \t\t[ {value} ]'.format(value=item.get('v'))) print() # print(response_1.get('data')) def main(): """ 主函數(shù) :return: None """ while True: try: query_keywords = input("""請(qǐng)輸入您要翻譯的內(nèi)容 [ 輸入四個(gè)'0'退出 ] : """) if query_keywords == "0000": # 如果輸入四個(gè) '0',退出小程序 print('########## 您已成功退出百度翻譯 ##########') break else: baidu = Baidu_Translate(query_string=query_keywords) baidu.parse_translate_data() except Exception as e: print('請(qǐng)求出錯(cuò),請(qǐng)重試', e.args) if __name__ == '__main__': main()
總結(jié)
以上所述是小編給大家介紹的基于python實(shí)現(xiàn)百度翻譯功能,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
opencv python Canny邊緣提取實(shí)現(xiàn)過程解析
這篇文章主要介紹了opencv python Canny邊緣提取實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02pyinstaller打包后,配置文件無(wú)法正常讀取的解決
這篇文章主要介紹了pyinstaller打包后,配置文件無(wú)法正常讀取的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02windows10安裝python依賴報(bào)錯(cuò)can‘t?create?or?remove?files?in?i
這篇文章主要介紹了windows10安裝python依賴報(bào)錯(cuò)can‘t?create?or?remove?files?in?install?directory問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2023-09-09兩行Python代碼實(shí)現(xiàn)pdf轉(zhuǎn)word功能
這篇文章主要為大家詳細(xì)介紹了如何利用兩行Python代碼就能實(shí)現(xiàn)pdf轉(zhuǎn)word功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03基于Python實(shí)現(xiàn)自動(dòng)關(guān)機(jī)小工具
上班族經(jīng)常會(huì)遇到這樣情況,著急下班結(jié)果將關(guān)機(jī)誤點(diǎn)成重啟,或者臨近下班又通知開會(huì),開完會(huì)已經(jīng)遲了還要去給電腦關(guān)機(jī)。今天使用PyQt5做了個(gè)自動(dòng)關(guān)機(jī)的小工具,設(shè)置好關(guān)機(jī)時(shí)間然后直接提交即可,需要的可以參考一下2022-10-10Python 實(shí)現(xiàn)平臺(tái)類游戲添加跳躍功能
這篇文章主要介紹了Python 實(shí)現(xiàn)平臺(tái)類游戲添加跳躍功能,,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03