intend
First install the crawler urllib library
pip install urllib
Get the link url for Yodo Translate
The parameters to be sent are in the form data
typical example
import import url = '/translate_o?smartresult=dict&smartresult=rule' data = {} data['i'] = 'i love python' data['from'] = 'AUTO' data['to'] = 'AUTO' data['smartresult'] = 'dict' data['client'] = 'fanyideskweb' data['salt'] = '16057996372935' data['sign'] = '0965172abb459f8c7a791df4184bf51c' data['lts'] = '1605799637293' data['bv'] = 'f7d97c24a497388db1420108e6c3537b' data['doctype'] = 'json' data['version'] = '2.1' data['keyfrom'] = '' data['action'] = 'FY_BY_REALTlME' data = (data).encode('utf-8') response = (url,data) html = ().decode('utf-8') print(html)
Running it will give you an error of 50, here you need to remove the _o from the url link
Run successfully after deletion
But this result still looks too complex and needs to be optimized in the
Import json, then convert to dictionary for filtering
import import import json url = '/translate?smartresult=dict&smartresult=rule' data = {} data['i'] = 'i love python' data['from'] = 'AUTO' data['to'] = 'AUTO' data['smartresult'] = 'dict' data['client'] = 'fanyideskweb' data['salt'] = '16057996372935' data['sign'] = '0965172abb459f8c7a791df4184bf51c' data['lts'] = '1605799637293' data['bv'] = 'f7d97c24a497388db1420108e6c3537b' data['doctype'] = 'json' data['version'] = '2.1' data['keyfrom'] = '' data['action'] = 'FY_BY_REALTlME' data = (data).encode('utf-8') response = (url,data) html = ().decode('utf-8') req = (html) result = req['translateResult'][0][0]['tgt'] print(result)
But this program can only translate one word, and it's useless when it runs out. So I'm optimizing
import import import json def translate(): centens = input('Enter the statement to be translated:') url = '/translate?smartresult=dict&smartresult=rule' head = {}# Add request headers to prevent anti-crawlers head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36' data = {}# Request with data from data data['i'] = centens data['from'] = 'AUTO' data['to'] = 'AUTO' data['smartresult'] = 'dict' data['client'] = 'fanyideskweb' data['salt'] = '16057996372935' data['sign'] = '0965172abb459f8c7a791df4184bf51c' data['lts'] = '1605799637293' data['bv'] = 'f7d97c24a497388db1420108e6c3537b' data['doctype'] = 'json' data['version'] = '2.1' data['keyfrom'] = '' data['action'] = 'FY_BY_REALTlME' data = (data).encode('utf-8') req = (url,data,head) response = (req) html = ().decode('utf-8') req = (html) result = req['translateResult'][0][0]['tgt'] # print(f'Result of Chinese-English translation: {result}') return result t = translate() print(f'Results of Chinese-English Interpretation:{t}')
Optimization is complete and works fine.
This is the whole content of this article.