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、百度翻译同理,请求也要修改

扫描二维码关注公众号,回复: 2908723 查看本文章

 

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)

猜你喜欢

转载自blog.csdn.net/csdn86868686888/article/details/82107675
今日推荐