使用Python寫了一個抓取有道詞典首頁的指令碼,目前功能還不完善,只能抓取基本的Tag並使用BeautifulSoup顯示出來,沒有對Tag裡的資訊進行細分。另外,網頁端是按utf-8編碼的,放到Windows上的命令列裡無法正常顯示,目前還沒有測試Linux終端裡的情況。有時間再完善一下吧。
#! /usr/bin/python# coding = utf-8# youdao.py# To-do: get meaning of a word from youdao.com# Author: Steven# Date: 2013/04/21import urllib, urllib2from bs4 import BeautifulSoupimport sysinputDecode = sys.stdin.encodingoutputDecode = sys.stdout.encodingdef getHtml(word = 'bless'):#word = raw_input("Please input your word:")youdaoHtml = 'http://dict.youdao.com/search'youdaoData = {'le': 'eng', 'q': word, 'keyfrom': 'dict'}youdaoRequest = urllib2.Request(youdaoHtml, urllib.urlencode(youdaoData))youdaoRequest.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)')wordHtml = urllib2.urlopen(youdaoRequest)return wordHtml.read().decode('utf-8')def showMeaning(word = 'bless'):word = raw_input('Please input your word:')wordSoup = BeautifulSoup(getHtml(word))# show the wordprint "Word:\t", wordSoup.find('span', {'class', 'keyword'}).string# show the pronounceprint "Prounaounciation:\t",for prons in wordSoup.find_all('span', {'class', 'phonetic'}):print prons.string.encode('utf-8'),# show meaningprint 'Meaning:\n'for meaning in wordSoup.find_all('span', {'class', 'def'}):print '***\t', meaning.string.encode(outputDecode)def sample(word = 'bless'):while 1:print "Input C to continue, input Q/q to quit.\n"selection = raw_input("Please input your command\n>>>")if selection in ['C', 'c']:showMeaning(word)elif selection in ['Q', 'q', 'quit', 'Quit']:breakelse:exit()if __name__ == '__main__':sample()