【问题标题】:Extract data from a website's list, without superfluous tags从网站列表中提取数据,没有多余的标签
【发布时间】: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


【解决方案1】:

我认为您正在寻找 findAll(text=True) 这将从标签中提取文本

definitions = soup('ul')[0].findAll(text=True)

将返回在标签边界处中断的所有文本内容的列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多