2

我正在使用这个模板标签:

@register.filter
def php_striptags(text, allowed=""):
    soup = BeautifulSoup(text)

    # list all tags
    allowed_tags = allowed.split()

    for tag in soup.find_all(True):
        if tag.name not in allowed_tags:
            tag.unwrap()

    return soup.encode_contents().decode('utf8')

它在开发机器上工作得很好,但我在生产中得到这个错误:

Exception Type:     RuntimeError
Exception Value:    restricted attribute
Exception Location:     /usr/local/lib/python2.7/inspect.py in getargspec, line 813

我在 webfaction 上托管我的网站,使用 apache 和 mod_wsgi 运行。有什么问题?

4

2 回答 2

4

Finally found the real issue, which actually was documented in BeautifulSoup doc: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#other-parser-problems

If your script works on one computer but not another, it’s probably because the two computers have different parser libraries available. For example, you may have developed the script on a computer that has lxml installed, and then tried to run it on a computer that only has html5lib installed. See Differences between parsers for why this matters, and fix the problem by mentioning a specific parser library in the BeautifulSoup constructor.

So to make your soup, try something like:

soup = BeautifulSoup(text, "html.parser")
于 2015-03-19T18:22:08.387 回答
0

我相信您没有将 BeautifulSoup 安装到您的系统 Python 库中,而是您已经下载了 BeautifulSoup,并将其放入您的自定义PYTHONPATH. 这使您的 BeautifulSoup 副本被视为外部 Python 代码,Python 以受限模式运行它(请参阅Restricted Execution

pip只需通过/安装 BeautifulSoup easy_install,或为其安装操作系统包,此错误就会消失。

于 2013-06-07T20:47:40.363 回答