AttributeError:"NoneType"对象在python 3.4中没有属性"lower"

编程入门 行业动态 更新时间:2024-10-27 03:41:02
本文介绍了AttributeError:"NoneType"对象在python 3.4中没有属性"lower"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

代码:

import requests from bs4 import BeautifulSoup import operator def start(url): word_list =[] source_code =requests.get(url).text soup = BeautifulSoup(source_code) for post_string in soup.find_all('a',{'class':'cb-skin-ads-link'}): content = post_string.string words = content.lower() for each_word in words: print(each_word) word_list.append(each_word) start('www.cricbuzz/live-cricket-scorecard/16445/gl-vs-rcb-qualifier-1-indian-premier-league-2016')

错误:

Traceback (most recent call last): File "C:/Users/Shera/PycharmProjects/Begin/wordcount.py", line 15, in <module> start('www.cricbuzz/live-cricket-scorecard/16445/gl-vs-rcb-qualifier-1-indian-premier-league-2016') File "C:/Users/Shera/PycharmProjects/Begin/wordcount.py", line 10, in start words = content.lower() AttributeError: 'NoneType' object has no attribute 'lower'

推荐答案

要了解发生了什么,我们应该看一下文档.在某些情况下,.string将是None:

To understand what is going on, we should take a look at the documentation. There is a case when .string would be None:

如果标签包含多个内容,则不清楚.string应该指的是什么,因此.string被定义为None

If a tag contains more than one thing, then it’s not clear what .string should refer to, so .string is defined to be None

您应该考虑使用 get_text() 还要考虑元素的子元素:

You should look into using get_text() instead that would take into account the children of an element as well:

content = post_string.get_text()

请注意,这将有助于避免该错误,但是由于找到的元素实际上没有任何文本,因此您仍然不会获得任何输出:

Note that this would help to avoid the error, but you would still get no output since the element you find really does not have any text:

<a target="_blank" href="Javascript:void(0)" class="cb-skin-ads-link cb-skin-ads-link-fixed ad-skin" rel="noreferrer"></a>

更多推荐

AttributeError:"NoneType"对象在python 3.4中没有属性"lower"

本文发布于:2023-11-23 11:08:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   quot   AttributeError   NoneType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!