【发布时间】:2012-06-02 02:00:58
【问题描述】:
工作代码:Google dictionary lookup via python and beautiful soup -> 只需执行并输入一个单词。
我很简单地从特定列表项中提取了第一个定义。然而,为了获得纯数据,我不得不在换行符处拆分数据,然后将其剥离以删除多余的列表标签。
我的问题是,有没有一种方法可以在不执行上述字符串操作的情况下提取特定列表中包含的数据——也许是我还没有看到的漂亮汤中的一个函数?
这是相关的代码部分:
# Retrieve HTML and parse with BeautifulSoup.
doc = userAgentSwitcher().open(queryURL).read()
soup = BeautifulSoup(doc)
# Extract the first list item -> and encode it.
definition = soup('li', limit=2)[0].encode('utf-8')
# Format the return as word:definition removing superfluous data.
print word + " : " + definition.split("<br />")[0].strip("<li>")
【问题讨论】:
-
所以...你想抓取一个网站?
-
嗯,这就是我的代码所做的。
-
我不知道什么是漂亮的肥皂,但我忍不住对它的缩写微笑
-
美汤是python的HTML/XML解析器。
标签: python html beautifulsoup extract