$cat youdao.py
#encoding:utf8
import sys
import urllib2
from lxml import etree
import json
import urllib
from optparse import optionparser
ua_header =
datadict["query"] = s
formdata = urllib.urlencode(datadict)
try:
request = urllib2.request(url,data=formdata,headers=ua_header)
response = urllib2.urlopen(request)
htmldata = response.read()
jsontext = json.loads(htmldata)
result = jsontext["trans_result"]["data"][0]["dst"]
if result ==none
or result ==:
print
"輸入錯誤,請檢視幫助"
print result
except exception as e:
print
"error:",e
#從命令列獲取你的輸入
defgetoptions
(): parser = optionparser()
parser.add_option('-t','--type',type='string',action='store',dest='type',
help=u"輸入型別,1 單詞詳解;2 語句翻譯")
parser.add_option("-c",'--contents',type="string",action='store',dest="contents",
help=u"輸入你的英文單詞或者語句")
options,args = parser.parse_args()
return options
defmain
(): enter = getoptions()
t = enter.type
contents = enter.contents
if'1'in t:
print contents,"\n翻譯如下:"
getwordtext(contents)
elif
'2'in t:
print contents,"\n翻譯如下:"
getsentencetext(contents)
if __name__=="__main__":
main()
如何操作:
1.安裝lxml庫
(1) pip 方式:
pip install lxml
(2) ubuntu下:
apt-get install python-lxml
(3) centos下:
yum install python-lxml
2.程式是python2編寫的,如下執行:
(1)獲取幫助:
[root#]python youdao.py -h
usage: youdao.py [options]
options:
-h, --help show this help message and exit
-t type, --type=type 輸入型別,1 單詞詳解;2 語句翻譯
-c contents, --contents=contents
輸入你的英文單詞或者語句
(2) 查詢單詞:
[root#]python youdao.py -t 1 -c beautiful
beautiful
翻譯如下:
adj. 美麗的
出色地出色的
迷人的迷人地
(3) 查詢語句:
[root#] python youdao.py -t 2 -c "i love you,you love him,he loves her"
i love you,you love him,he loves her
翻譯如下:
我愛你,你愛他,他愛她。
3.使用別名:
(1)在 ~/.bashrc最後一行新增如下:
alias fyi="/usr/bin/python /home/jokerzhang/scripts/fanyi/youdao.py -t"
(2)執行命令如下:
source 或 . .bashrc
(3) 這樣就可以簡化操作了:
fyi 1 -c hello
尋找百度翻譯引數,實現百度翻譯
author runsen 首先要分析瀏覽器是怎麼傳送請求的。構造請求 找到關鍵請求之後,就要分析請求,然後去構造請求。分析請求有一下幾個要素,url,請求方法,請求頭,請求引數。請求引數有七個,可以通過反覆請求然後比較的方法得出from,to是表示翻譯語言的,transtype,means fla...
spider pc版百度翻譯介面 post方式
分析過程,略 有時間再補 python usr bin env python coding utf 8 需要的庫有 js2py,requests,re,json author yxq import js2py import requests import json import re url fan...
access百度翻譯 get 我的百度翻譯
為了便於使用,寫了乙個簡單但夠用的使用者介面,它長這個樣子 在左邊輸入英文按下回車後,右邊就會顯示翻譯結果了。這個東西只是單純的顯示翻譯結果,像音標 發音 中英文對照等功能都沒有,而且編輯不方便 如提供乙個 按鈕來清空 不過,其中的一些是api本身不支援 如文字轉語音 另外的如加音標 方便操作的功能...