2011-04-21 14 views
0

我有特定的用途。我正在準備GRE。每當有新單詞出現時,我會在 www.mnemonicdictionary.com上查找它的含義和助記符。我想用python編寫一個腳本(或者如果有人可以給我一個指向已經存在的東西的指針,因爲我不知道python,但我現在正在學習),它從文本文件中獲取單詞列表,並查找它在這個網站上,只需獲取相關部分(含義和助記符)並將其另存爲另一個文本文件以供離線使用。是否有可能這樣做?我也試圖查找這些網頁的來源。但隨着html標籤,他們也有一些Ajax功能。 有人可以提供一個完整的方式如何去做這個?編寫一個腳本來獲取一個循環中的網頁的特定部分以供離線使用

舉例:字身無分文:

相關的HTML源代碼是這樣

<ul class='wordnet'><li><p>(adj.)&nbsp;not having enough money to pay for necessities</p><u>synonyms</u> : <a href='http://www.mnemonicdictionary.com/word/hard up' onclick="ajaxSearch('hard up','click'); return false;">hard up</a> , <a href='http://www.mnemonicdictionary.com/word/in straitened circumstances' onclick="ajaxSearch('in straitened circumstances','click'); return false;">in straitened circumstances</a> , <a href='http://www.mnemonicdictionary.com/word/penniless' onclick="ajaxSearch('penniless','click'); return false;">penniless</a> , <a href='http://www.mnemonicdictionary.com/word/penurious' onclick="ajaxSearch('penurious','click'); return false;">penurious</a> , <a href='http://www.mnemonicdictionary.com/word/pinched' onclick="ajaxSearch('pinched','click'); return false;">pinched</a><p></p></li></ul> 

但網頁呈現這樣的:

•(ADJ)沒有足夠的錢支付必需品 同義詞:hard up,in straitened circumstances,penniless,penurious,pinched

回答

1

使用curl和sed從Bash shell(Linux,Mac或Windows與Cygwin)。

如果我得到第二個我會寫一個快速的腳本...現在得給嬰兒洗澡。

3

如果你有擊(4.0以上的版本)和wget,一個例子

#!/bin/bash 
template="http://www.mnemonicdictionary.com/include/ajaxSearch.php?word=%s&event=search" 
while read -r word 
do 
    url=$(printf "$template" "$word") 
    data=$(wget -O- -q "$url") 
    data=${data#*&nbsp;} 
    echo "$word: ${data%%<*}" 
done < file 

樣本輸出

$> more file 
synergy 
tranquil 
jester 

$> bash dict.sh 
synergy: the working together of two things (muscles or drugs for example) to produce an effect greater than the sum of their individual effects 
tranquil: (of a body of water) free from disturbance by heavy waves 
jester: a professional clown employed to entertain a king or nobleman in the Middle Ages 

更新:包括mneumonic

template="http://www.mnemonicdictionary.com/include/ajaxSearch.php?word=%s&event=search" 
while read -r word 
do 
    url=$(printf "$template" "$word") 
    data=$(wget -O- -q "$url") 
    data=${data#*&nbsp;} 
    m=${data#*class=\'mnemonic\'} 
    m=${m%%</p>*} 
    m="${m##*&nbsp;}" 
    echo "$word: ${data%%<*}, mneumonic: $m"  
done < file 
+0

非常感謝。這是如此真棒...使用ajaxsearch.php ..你只是檢索頁面的一部分..我不知道我們可以做那 – avd 2011-04-21 02:00:08

+0

你也可以告訴我你操作數據變量時使用哪些操作符來獲取相關部分?我在哪裏可以瞭解它們,以便我修改它以檢索助記符。 – avd 2011-04-21 02:32:28

+0

你可以查看tldp.org/LDP/abs/html/string-manipulation.html – 2011-04-21 02:35:20

相關問題