为什么我的网站刮擦功能返回出乎意料的东西?
我的目标:试图构建功能; def retireve_title(html)
期望为输入,一串HTML并返回标题元素。
我已经进口美丽的小组来完成此任务。当我仍在学习时,任何指导都将受到赞赏。
我的尝试函数:
def retrieve_title(html):
soup = [html]
result = soup.title.text
return(result)
使用功能:
html = '<title>Jack and the bean stalk</title><header>This is a story about x y z</header><p>talk to you later</p>'
print(get_title(html))
意外结果:
“ attributeError:'列表'对象没有属性'title'
预期结果:
“杰克和豆stal”
My goal: Attempting to build a function; def retrieve_title(html)
that expects as input, a string of html and returns the title element.
I've imported beautifulsoup to complete this task. Any guidance is appreciated as I'm still learning.
My attempted function:
def retrieve_title(html):
soup = [html]
result = soup.title.text
return(result)
Using the function:
html = '<title>Jack and the bean stalk</title><header>This is a story about x y z</header><p>talk to you later</p>'
print(get_title(html))
Unexpected outcome:
"AttributeError: 'list' object has no attribute 'title'"
Expected outcome:
"Jack and the beanstalk"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标签后立即进行文本节点
jack和bean stalk
是标题 >输出:
Jack and the bean stalk
is a text node immediate aftertitle tag
so to grab that you can apply.find(text=True)
Output:
您必须调用功能
You have to call the function