python - 什么是美汤绑法?

标签 python django beautifulsoup robobrowser

我正在尝试 http://robobrowser.readthedocs.org/en/latest/readme.html ,一个基于 beautiful soup 库的新 python 库。我试图通过打开一个 html 页面并在 django 应用程序中返回它来测试它,但我不知道如何完成这个最简单的任务。我的 django 应用程序包含:

def index(request):    

    p=str(request.POST.get('p', False)) # p='https://www.yahoo.com/'
    browser = RoboBrowser(history=True)
    browser.open(p)
    html = browser.find_all
    return HttpResponse(html)

当我查看输出的 html 时,我看到:

<bound method BeautifulSoup.find_all of 
    <!DOCTYPE html>
    <html>
    ......................
        <head>
    ...............
        </body>
    </html>
>

什么是美汤绑法?我怎样才能得到直接的html?

最佳答案

它是一个方法对象,绑定(bind)到 BeautifulSoup 对象。您没有调用它。

它的表示有点令人困惑,因为包含了 BeautifulSoup 解析树的 repr(),它只是呈现为 HTML 源字符串的树。

要获取底层的 BeautifulSoup 解析树,您可以使用;使用 str() 将其转回源字符串:

html = str(browser.state.parsed)

或者,您仍然可以通过以下方式访问原始 requests 响应对象:

browser.state.response

这意味着原始下载的 HTML 被发现为:

html = browser.state.response.content

关于python - 什么是美汤绑法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414369/

相关文章:

python - 如何在 Python ClientForm 中设置一个不存在的字段?

python - 替换列表的简单 Bootstrap

django - 如何创建不允许编辑某些字段的自定义 UserChangeForm?

django - 如何在 Azure 应用服务上(连续)运行工作线程

python - 如何使用 AWS Cognito - 身份池检索正确的凭证以在 boto3 客户端上访问 AWS SecretsManger

python - 如何合并两组从 Vivino.com 抓取的信息

django - 在使用多对多关系之前,实例需要有主键值

python - BeautifulSoup 的多个标签

python - 如何使用 BeautifulSoup 获取 colindex 数字?

Python 正在过滤掉货币标记