PYTHONクローラーBing辞書翻訳クロール



Python Crawler Bing Dictionary Translation Crawl



PYTHONクローラーBing辞書翻訳クロール
英語-中国語と中国語-英語の翻訳を同時に実現
ブラウザを右クリック->チェック-> URLを検索...など。
コードをアップロード
方法1(ユーザー入力と必要な操作による翻訳)

import json import requests url='https://cn.bing.com/tlookupv3?isVertical=1&&IG=2DE380192315479AA9B845D31A4CAEC6&IID=translator.5028.2' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36' } lan=int(input('Chinese to English input 1 English to Chinese input 2:')) word=input('Please enter the content to be translated:') if(lan==1): formdata={ 'from':'zh-Hans', 'to':'en', 'text':word } elif(lan==2): formdata={ 'from': 'en', 'to': 'zh-Hans', 'text': word } try: r = requests.post(url,data=formdata,headers=headers) r.raise_for_status() r.encoding =r.apparent_encoding data=json.loads(r.text) result=data[0]['translations'][0]['normalizedTarget'] print(result) except Exception as e: print('Error', e)

方法2英語から中国語に翻訳するか、中国語から英語に翻訳するかを自動的に決定します



import json import requests url='https://cn.bing.com/ttranslatev3?isVertical=1&&IG=ED0206E205E2433A9D478DB419F3CC7F&IID=translator.5028.2' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36' } word=input('Please enter the content to be translated').strip() #Remove the first and last spaces #Judge the translation to Chinese or English # Determine whether the first letter is English if 'A'<=word[0]<='Z' or 'a'<=word[0]<='z': to='zh-Hans' else: to='en' # Determine whether the first letter is Chinese #if 'u400'<=word[0]<='u9fa5': # to='en' #else: # to='zh-Hans' formdata={ 'fromLang': 'auto-detect', 'to':to, 'text': word } try: r = requests.post(url,data=formdata,headers=headers) r.raise_for_status() r.encoding =r.apparent_encoding data=json.loads(r.text) result=data[0]['translations'][0]['text'] print(result) except Exception as e: print('Error', e)

予防:
ウェブページのURLを表示するときは、翻訳結果をネットワークで検索して、対応するURLがヘッダーで取得されるようにする必要があります。