日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 百度词典_python在线抓取百度词典的翻译结果翻译单词

發布時間:2024/9/20 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 百度词典_python在线抓取百度词典的翻译结果翻译单词 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這段代碼通過抓取百度詞典的翻譯結果達到翻譯單詞的目的

這個小工具使用Python語言編寫完成,其中使用到這 些類庫(urllib,BeautifulSoup ),前者主要負責網絡通訊方面,后者負責HTML的解析。這也是Python語言生態圈的強大之處,寫個這樣的小工具,毫不費力。

在線翻譯的原理:首先根據用戶輸入的單詞提交給百度詞典 ,其次讀取百度詞典返回的數據并解析,最后將處理過的數據顯示給用戶。以下是該工具的具體代碼(Translate.py)

import urllib

import codecs

from BeautifulSoup import BeautifulSoup

from sys import argv

import re,time

class Translate:

def Start(self):

self._get_html_sourse()

self._get_content("enc")

self._remove_tag()

self.print_result()

def _get_html_sourse(self):

word=argv[1] if len(argv)>1 else ''

url="http://dict.baidu.com/s?wd=%s&tn=dict" % word

self.htmlsourse=unicode(urllib.urlopen(url).read(),"gb2312","ignore").encode("utf-8","ignore")

def _get_content(self,div_id):

soup=BeautifulSoup("".join(self.htmlsourse))

self.data=str(soup.find("div",{"id":div_id}))

def _remove_tag(self):

soup=BeautifulSoup(self.data)

self.outtext=''.join([element for element in soup.recursiveChildGenerator() if isinstance(element,unicode)])

def print_result(self):

for item in range(1,10):

self.outtext=self.outtext.replace(str(item),"\n%s" % str(item))

self.outtext=self.outtext.replace(" ","\n")

print self.outtext

#from sharejs.com

if __name__=="__main__":

Translate().Start()

總結

以上是生活随笔為你收集整理的python 百度词典_python在线抓取百度词典的翻译结果翻译单词的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。