我跟有道翻譯幹起來了
1.先把需要用的url和queryString以及報頭準備好
<pre>url = http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
data={
"i":"python%0A",
"from":"AUTO",
"to":"AUTO",
"smartresult":"dict",
"salt":"1506836328053",
"sign":"945d7dd737a4fb4327914ff3f3bf7d90",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_ENTER",
"typoResult":"true"
}
</pre>
2.然後就一早晨跟有道網頁翻譯開始無休止的戰鬥,被一次次地教做人。。。。
下面是親生的代碼:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import urllib
import urllib2
##url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom="
下面是用上面這個Url進行操作返回的,感覺不全面。
##>>>
##Enter the word:中國
##{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":0,"translateResult":[[{"src":"中國","tgt":"China"}]],"smartResult":{"type":1,"entries":["","China"]}}
<font color=red>#不能用url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom="這個url,這個我懷疑是onlyonce的意思</font>
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom"
這個是用另一個url進行操作返回的結果,能稍微全面一點。
##>>>
##Enter the word:python
##{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"python","tgt":"python"}]],"smartResult":{"type":1,"entries":["","n. 巨蟒;大蟒","n. (法)皮東(人名)"]}}
##head="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
queryword = raw_input("Enter the word:")
##data = {}
##data["i"] = queryword
##data["from"]="AUTO"
##data["to"]="AUTO"
##data["smartresult"]="dict"
##data["client"]="fanyideskweb"
##data["salt"]="502865709143"
##data["sign"]="e7b725d55dd02ab7b3a17c44170950ad"
##data["doctype"]="json"
##data["version"]="2.1"
##data["keyfrom"]="fanyi.web"
##data["action"]="FY_BY_CLlCKBUTTON"
##data["typoResult"]="true"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
}
data={
"i":queryword,
"from":"AUTO",
"to":"AUTO",
"smartresult":"dict",
"client":"fanyideskweb",
"salt":"1506836328053",
"sign":"945d7dd737a4fb4327914ff3f3bf7d90",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_ENTER",
"typoResult":"true"
}
data = urllib.urlencode(data).encode("utf-8")
#request = urllib2.Request(url,data,header)
request = urllib2.Request(url,data=data,headers=header)
##response = urllib2.urlopen(request)
html=urllib2.urlopen(request).read().decode("utf-8")
#html = urllib2.urlopen(request).read().decode("utf-8")
print html
下面是網上找的代碼:
# -*- coding: utf-8 -*-
import urllib
import urllib2
import json
#Request URL
while 1:
i = raw_input("請輸入要翻譯的文字:")
if i==‘!q‘:
print("退出")
break
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom="
data ={}
#方案一
#head={}
#head["User-Agent"]="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
#方案二
head="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
# From Data 全部屬性
data["i"]=i
data["from"]="AUTO"
data["to"]="AUTO"
data["smartresult"]="dict"
data["client"]="fanyideskweb"
data["salt"]="502865709143"
data["sign"]="e7b725d55dd02ab7b3a17c44170950ad"
data["doctype"]="json"
data["version"]="2.1"
data["keyfrom"]="fanyi.web"
data["action"]="FY_BY_CLlCKBUTTON"
data["typoResult"]="true"
#轉碼
data =urllib.urlencode(data).encode("utf-8")
#打開鏈接
#方案一
#req = urllib.request.Request(url,data,head)#Request設置
#方案二
req = urllib2.Request(url,data)
req.add_header("User-Agent",head)
response = urllib2.urlopen(req)
#轉為Unicode
html=response.read().decode("utf-8") #輸出為json格式
#json文件讀取
target = json.loads(html)
#最終字典列表輸出
print(target["translateResult"][0][0]["tgt"])
總結。。。。。。。。
一個初學者的血淚,忙活了一早上,查各種資料,各種改代碼,最後被一個小寫的o給幹翻了。。。,有"="跟沒有“=”這貨是不一樣的。心酸。
心中萬馬奔騰,但是這個X還是要裝的。
本文出自 “phize” 博客,請務必保留此出處http://12756301.blog.51cto.com/12746301/1970087
我跟有道翻譯幹起來了