【问题标题】:What is wrong with the bs4 documentation? I can't run unwrap() sample codebs4 文档有什么问题?我无法运行 unwrap() 示例代码
【发布时间】:2012-10-26 22:32:21
【问题描述】:

我正在尝试从诸如this 之类的页面中删除一些繁琐的文本。我想保留锚定链接但丢失中断和 a.intro。我以为我可以使用 unwrap() 之类的东西来剥离层,但出现错误:TypeError: 'NoneType' object is not callable

为了好玩,我尝试运行文档示例代码本身,因为我看不出我的版本有何不同。

markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
a_tag = soup.a

a_tag.i.unwrap()
a_tag
# <a href="http://example.com/">I linked to example.com</a>

我得到了完全相同的错误。我在这里想念什么?我在 Scraperwiki 工作,fwiw。

【问题讨论】:

  • 这似乎是一个 Scraperwiki 问题。例如,unwrap 示例在从 IPython 运行时有效。

标签: python web-scraping beautifulsoup scraperwiki


【解决方案1】:

似乎是一个scraperwiki 问题。在 ipython 控制台中工作正常。

【讨论】:

    【解决方案2】:

    我也遇到这个错误。

    In [27]: type(a_tag.i.unwrap)
    Out[27]: NoneType
    
    In [28]: 'unwrap' in dir(a_tag.i)
    Out[28]: False
    

    FWIW,replace_with_children 产生相同的结果:

    In [29]: type(a_tag.i.replace_with_children)
    Out[29]: NoneType
    

    在我看来是个错误。

    In [13]: import BeautifulSoup as Bs
    
    In [16]: Bs.__version__
    Out[16]: '3.2.1'
    

    【讨论】:

      【解决方案3】:

      soup.select() 出现了相同的错误消息。原因是 BeautifulSoup4 库的旧版本。 ScraperWiki 的某个人修复了它(参见 ScraperWiki Google Group 的 this conversation)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多