【问题标题】:Escaping … with BeautifulSoup逃避……用 BeautifulSoup
【发布时间】:2011-03-10 11:59:05
【问题描述】:

我目前正在使用 BeautifulSoup 抓取一些网站,但是我遇到了一些特定字符的问题,UnicodeDammit 中的代码似乎(再次)表明这是一些微软发明的。

我正在使用最新版本的 BeautifulSoup(3.0.8.1),因为我仍在使用 python2.5

以下代码说明了我的问题:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('...Baby One More Time (Digital Deluxe Version…')
print soup

'...Baby One More Time (Digital Deluxe Version…'

如您所见,问题在于末尾的 '...'(&hellip) 字符(您的浏览器可能正确转义了该字符)。显然这不是我感兴趣的。

如果有这个字符的 unicode 表示或其他东西会很好。 即使只是简单地忽略它也会解决我的特殊问题。

BeautifulSoup 如何做到这一点?

【问题讨论】:

    标签: python escaping beautifulsoup web-scraping


    【解决方案1】:

    MS 可能发明了它,但… 是 HTML 4 的一部分:http://www.w3.org/TR/REC-html40/sgml/entities.html

    也许您的Lib/htmlentitydefs.py 丢失或过时,因为这是 BeautifulSoup 用来转换实体的。

    如果您查看the Python 2.5 source tree,您会清楚地看到它在第 126 行定义。

    【讨论】:

      【解决方案2】:

      自己找到了解决方案:

      soup = BeautifulSoup('...Baby One More Time (Digital Deluxe Version…', convertEntities="html")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-05
        • 2018-06-14
        • 2011-06-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 2020-02-14
        • 2011-07-01
        相关资源
        最近更新 更多