Application of post() method in python in obtaining translation results of Bing website

Obtain the translation results of the Bing website. Note that both cases of Chinese translation into English and English translation into Chinese need to be considered.
URL: https://cn.bing.com/translator/
1. Analyze the webpage. It
It is recommended to enter a sentence directlyis recommended to open the webpage to check the network when it is blank, and then directly enter a sentence, so that there are fewer packages loaded on the right side and it is easy to find the target URL.
nono
This is the original request parameter, so the data parameter can be constructed in this format. data:{'fromLang':'auto-detect','text':original text,'to':'zh-Hans'}, that's probably it. I don’t want to know the specific meaning of “fromLang”: “auto-detect” here. It may indicate the meaning of a sentence. I tried to translate it from Chinese to English to Chinese, so I just let it go.
'text': Original text, needless to say, this is the parameter for saving the original text.
'to':'zh-Hans' This means what the translation is, zh-Hans means simplified Chinese, en means English, here is only Chinese-English-English-Chinese translation, in fact, other languages ​​have other parameters.
Another point is this URL. It can translate a word by word, or it can directly translate a sentence. I am here to directly translate a sentence. Let’s put it this way, it’s best to translate a sentence directly, because the translation A sentence can translate a single word, and a single word may not necessarily translate a sentence. At most, the first letter is capitalized when translating from Chinese to English. Other problems should be minor.
Because the parameters that control the translation are different when translating between Chinese and English, you need to determine whether the input is a Chinese character or a word. This is used here.
\u4e00 ~ \u9fff
This is the encoding range of Chinese characters. Nothing else.
Two, the code

import requests
def start_tran():
    content = input('需要翻译的内容呀:\n')
    from_data = {
    
    }
    for i in content:
        if '\u4e00'<= i <= '\u9fff':
            print('您输入的是中文!!!')
            from_data = {
    
    'fromLang':'auto-detect','text':content,'to':'en'}
            break
        else:
            print('您输入的是英文!!!')
            from_data = {
    
    'fromLang':'auto-detect','text': content, 'to': 'zh-Hans'}
            break
    return from_data
def translate(from_data):
    url = 'https://cn.bing.com/ttranslatev3?isVertical=1&&IG=FFEB3453664B412382446351E8F72096&IID=translator.5028.63'
    headers = {
    
    
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
    r = requests.post(url,data = from_data ,headers=headers)
    r.raise_for_status()
    #print(r.status_code)
    r.encoding = r.apparent_encoding
    data = r.json()  # 取出翻译结果
    detectedLanguage = data[0]
    translations = detectedLanguage['translations']
    result = translations[0]['text']
    print(result)
translate(start_tran())

Is the JAVA code block easy to use (whispering bb)
code running result

需要翻译的内容呀:
我能送你回家吗,外面可能要下雨啦。
您输入的是中文!!!
Can I take you home? It may rain outside.

English-Chinese translation works equally well.

Guess you like

Origin blog.csdn.net/qq_46295527/article/details/104830280