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.) 没有足够的钱来支付必需品的同义词:艰难,处境困难,身无分文,一文不名,一筹莫展

4

2 回答 2

3

如果你有 Bash(版本 4+)和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

更新:包括助记符

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
于 2011-04-21T01:16:39.047 回答
1

在 Bash shell(Linux、Mac 或带有 Cygwin 的 Windows)中使用curl和 sed。

如果我有时间,我会写一个快速的脚本……不过现在得给宝宝洗个澡。

于 2011-04-21T01:01:56.113 回答