我正在尝试 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/