Python爬虫实现有道翻译

发布时间:2024-12-12 07:36

Python爬虫实战:requests库应用 #生活知识# #编程教程#

最新推荐文章于 2024-05-03 11:46:34 发布

 盖世英雄 于 2019-03-05 11:47:08 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

# -*- coding: utf-8 -*- #模拟有道翻译 import urllib import urllib2 #通过抓包得到的url地址 url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule " #完整的headers headers={ #"Host":"fanyi.youdao.com", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:65.0) Gecko/20100101 Firefox/65.0", "Accept":"application/json, text/javascript, */*; q=0.01", "Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8", "X-Requested-With":"XMLHttpRequest", #"Connection":"keep-alive" } #输入关键字 只能英译中 kkk~ key=raw_input("请输入需要翻译的文字:") #发送到web服务器的表单数据 formdata={ "type":"AUTO", "i":key, "doctype" :"json", "version" : "2.1", "keyfrom" : "fanyi.web", "ue":"UTF-8", "action" : "FY_BY_REALTIME", "typoResult" : "true" } #转码 data=urllib.urlencode(formdata) #请求 Request()方法里有值请求为POST 没有则为GET request=urllib2.Request(url,data=data ,headers=headers) reponse=urllib2.urlopen(request) print reponse.read()

12345678910111213141516171819202122232425262728293031323334353637

但是这个方法只能英译中
其原因应该是"type":"EN2ZH_CN"代表英2中 这个type找不到哪里修改

代码曾遇到的问题
1.{"errorcode":50}
解决方法url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule "将translate_o里的_o去掉变为url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule "即可
2.urllib2.HTTPError: HTTP Error 502: Bad Gateway
解决方法把传到数据单的typoResult标签置为true"typoResult" : "true"
所有有关网页的信息都是用fliddler抓包抓下来的分析了RAW WebForms,查看了JSON
运行结果

请输入需要翻译的文字:peace {"type":"EN2ZH_CN","errorCode":0,"elapsedTime":0,"translateResult":[[{"src":"peace","tgt":"和平"}]]} 123

网址:Python爬虫实现有道翻译 https://www.yuejiaxmz.com/news/view/450589

相关内容

python爬虫代码
python爬虫
Python爬虫抓取基金数据分析、预测系统设计与实现——云诺说
学了python究竟有什么用,实际应用场景有哪些?我整理了8个应用领域
​5 个 Python 代码来自动化你的日常工作,网友:早知道就好了
有道翻译API
python爬取B站千万级数据,发现了这些热门UP主的秘密!
Python爬虫山东济南景点数据可视化和景点推荐系统 开题报告
python基于天气预测的生活规划系统的设计与实现
推荐这三款自动化爬虫软件,非常实用!

随便看看