python3爬虫实现翻译

参考 https://blog.csdn.net/c406495762/article/details/59095864

 1、获得有道翻译的请求,实际请求那个_o要删除。

2、要提交的表单数据

3、拿到翻译的结果

实际的代码如下,有些表单数据可以不提交,提交和得到的响应记得都要转码为utf-8:

def youdaoTranslte(content):
    Request_URL = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    Form_Data = {}    
    Form_Data['doctype'] = 'json'#can't delete,return type   
    Form_Data['i'] = content#can't delete,what you want to translate   
    data = parse.urlencode(Form_Data).encode('utf-8')
    response = request.urlopen(Request_URL,data)
    html = response.read().decode('utf-8')  
    translate_results = json.loads(html)   
    translate_results = translate_results['translateResult'][0][0]['tgt']
    print("the translate result is:%s" % translate_results)

4、百度翻译同理,请求也要修改

 

def baiduTranslate(content):
    Request_URL='http://fanyi.baidu.com/transapi'
    Form_data={'from':'zh','query':content,'to':'en'}
    data=parse.urlencode(Form_data).encode('utf-8')
    response=request.urlopen(Request_URL,data)
    html=response.read().decode('utf-8')
    translate_results = json.loads(html)
    translate_results = translate_results['data'][0]['dst']
    print("the translate result is:%s" % translate_results)

 

 

版权声明:本文为csdn86868686888原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/csdn86868686888/article/details/82107675