使用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/21 import urllib, urllib2 from bs4 import BeautifulSoup import sys inputDecode = sys.stdin.encoding outputDecode = sys.stdout.encoding def 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 word print "Word:\t", wordSoup.find('span', {'class', 'keyword'}).string # show the pronounce print "Prounaounciation:\t", for prons in wordSoup.find_all('span', {'class', 'phonetic'}): print prons.string.encode('utf-8'), # show meaning print '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']: break else: exit() if __name__ == '__main__': sample()