python写的翻译脚本
2018-07-20 来源:open-open
# -*- coding:utf-8 -*-
#!/usr/bin/env python
import re
import urllib
import urllib2
import sys
class dict :
def __init__(self):
reload(sys)
sys.setdefaultencoding('utf8')
def serach(self):
waitWord = raw_input("输入要查询的内容:")
waitWord = urllib.quote(waitWord)
baiduUrl = "http://dict.youdao.com/search?q="+waitWord+"&keyfrom=dict.index"
userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0'
headers = {'User-Agent':userAgent}
req = urllib2.Request(baiduUrl,headers = headers)
Res = urllib2.urlopen(req)
ResultPage = Res.read().decode("utf-8")
show = re.findall(r'<div class="trans-container">.*<div id="webTrans" class="trans-wrapper trans-tab">',ResultPage,re.S)
try:
s = show[0].decode("utf-8")
pos = re.findall(r'<li>.*<\/li>',s,re.S)
arr = pos[0].split('\n')
if len(arr)>0:
for x in arr:
print re.sub('<[^>]+>','',x).strip()
except :
print '没有查到'
if __name__ == '__main__':
mydict = dict()
mydict.serach()
标签: linux
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐