繁体   English   中英

如何修复“AttributeError: 'str' object has no attribute 'content'”python 错误

[英]how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error

这是我使用的代码。

n=0
link = coverpage_news[n]['href']
title = coverpage_news[n].get_text() 
article = url.read() 
article_content = article.content 
soup_article = BeautifulSoup(article_content, 'html5lib' 

但它显示一条错误消息“AttributeError:'str'对象没有属性'内容',请谁能帮助我。

当您将 html 源代码输入到 BeautifulSoup 对象中时,您希望将其作为字符串输入。 由于您在使用url.read()时已经将其作为字符串,因此无需将其转换为带有.content的字符串,因此出现错误, str上没有content属性。 只需删除该行即可解决问题。

n=0
link = coverpage_news[n]['href']
title = coverpage_news[n].get_text() 
article_content = url.read() 
soup_article = BeautifulSoup(article_content, 'html5lib')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM