[Python crawler] Simple implementation of Chinese-English translator

Web analytics

Open Youdao Translation, use the translation function, F12 to open the Network, you will find a lot of get and post data, find the data requested for the translated content, as shown in the figure below,
Insert picture description here
simply analyze the content of the header
Insert picture description here

Two ways to add header

1) Modify the header parameter of the request
2) Modify the add_header() method of the request

Get the translation result according to the returned content
Insert picture description here

Implementation code

import urllib.request as req
import urllib.parse
import json

while True:
    content = input("请输入需要翻译的内容:(输入!q退出)")
    if content == '!q':
        break
    # 输入请求URL
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom='
    '''
    添加header方法1
    head = {}
    head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    '''
    data = {
    
    }
    # 将请求表单以字典的形式写入data传入,i表示需要翻译的内容
    # data['i'] = '今天也是元气满满的一天呀'
    data['i'] = content
    data['from'] = 'AUTO'
    data['to'] = 'AUTO'
    data['smartresult'] = 'dict'
    data['client'] = 'fanyideskweb'
    data['salt'] = '16099982906531'
    data['sign'] = 'a9d35d5d61ef0421160b9d53b6dff04f'
    data['lts'] = '1609998290653'
    data['bv'] = '4f7ca50d9eda878f3f40fb696cce4d6d'
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_CLICKBUTTION'
    data = urllib.parse.urlencode(data).encode('utf-8')
    request = req.Request(url, data)
    # 添加header方法2
    request.add_header('User-Agent',
                       'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')
    response = req.urlopen(request)
    html = response.read().decode('utf-8')
    # 根据返回内容获取翻译结果
    target = json.loads(html)
    target = target['translateResult'][0][0]['tgt']
    print(target)

Test result:
Insert picture description here
the difference between requests library and urllib library

Guess you like

Origin blog.csdn.net/qq_36477513/article/details/112299615