【发布时间】:2018-01-25 20:31:36
【问题描述】:
我是一个业余的 Python 用户。目前我正在尝试找出漂亮的汤模块,但我无法通过 select 方法找到任何东西。 我已经制作了一个示例 html 文件(或多或少从“用 python 自动化无聊的东西”一书中复制),其内容是:
<html><head><title>The Website Title</title></head>
<body>
<p><strong>Hi There!</strong> here is a link to a website: <a href="http://
inventwithpython.com">a website thing</a>.</p>
<p class="slogan">this is a roundup, this is a low flying panic attack.</p>
<p>By <span id="author">Yonatan.</span></p>
</body></html>
我已经在 shell 中输入了这段代码:
examplefile = open('example.html')
examplesoup = bs4.BeautifulSoup(examplefile.read())
elem = examplesoup.select('#author')
但我得到的 elem 是一个空列表。我已经检查了 examplefile.read() 和它的真实情况。还尝试了 select('p') 并没有得到任何结果。 有什么很明显的东西我在这里想念的吗?我也是 html 新手。
【问题讨论】:
-
这对我来说很好用。你的文件内容有问题。我希望你在做
print(elem)。 -
我是。我的文件内容有问题是什么意思? html还是脚本?顺便说一句,它在通过 .text 传递的请求对象上效果很好。
-
'example.html'...
标签: python html ubuntu beautifulsoup python-3.5