命令行字典

命令行字典

作爲一個有追求的程序員,當然想把一切操作通過鍵盤來完成,讓手指保持在鍵盤上。那麼,如何做到用鍵盤來查詢英文單詞呢?突發奇想在命令行裏用python腳本來爬取有道詞典的查詢結果,使用requests + BeautifulSoup實現,效果還不錯。

代碼

代碼放在了heLomaN@Github

#!/usr/bin/env python
# coding=utf-8
import requests as rq
from BeautifulSoup import BeautifulSoup as bs

def query(word):
    query_dict = {'le':'eng', 'q':word}
    r = rq.get('http://dict.youdao.com/search', query_dict)

    soup = bs(r.text)
    trans_div = soup.find('div', {'class':'trans-container'})

    #print trans_div
    interpretations = trans_div.findAll('li')
    for elem in interpretations:
        print '\t', elem.string


if __name__ == '__main__':
    word = raw_input('Query:\t')
    while(word != 'q'):
        query(word)
        word = raw_input('Query:\t')
  • 執行腳本後,輸入待查詢單詞即可,輸入q可退出執行
  • 機制非常簡單,構造查詢url,獲得html後找到對應於釋義的div,打印其中內容

 
 

轉載請註明作者:Focustc,博客地址爲http://blog.csdn.net/caozhk,原文鏈接爲點擊打開
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章