【问题标题】:Python beautiful Soup code not workingPython漂亮的汤代码不起作用
【发布时间】:2011-06-08 13:23:54
【问题描述】:

我正在 linux 中尝试此代码并给出空输出。 我刚刚处理并粘贴了 BS 文档

import urllib2
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup("""Bob's <b>Bold</b> Barbeque Sauce now available in
                        <b class="hickory">Hickory</b> and <b class="lime">Lime</a>""")

soup.find("b", { "class" : "lime" })

输出为空

【问题讨论】:

  • soup.find("b", { "class" : "lime" }).contents 似乎工作得很好

标签: python linux beautifulsoup


【解决方案1】:

除非您print,否则您的脚本不会输出任何内容。你需要...

print soup.find("b", { "class" : "lime" })

(这在这里工作。)

【讨论】:

  • 我试过类似for anchor in soup.findall('a', { "class" : "active" } ):print anchor['href']的东西,但效果不佳
  • 如果您添加print,您发布的脚本是否仍然不输出任何内容?
【解决方案2】:

它适用于 Python 2.6 或 2.7 和 BeautifulSoup 3.2.0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2020-03-22
    • 2022-12-03
    • 2020-03-15
    • 2014-03-20
    相关资源
    最近更新 更多