项目场景:
python爬虫爬取小说(Jack cui网络爬虫教学实例)问题描述:
遇到的问题:
代码编译后出现AttributeError: ‘NoneType’ object has no attribute 'text’错误
Traceback (most recent call last):
File "E:/Python/src/sd.py", line 28, in <module>
content=get_content(url)
File "E:/Python/src/sd.py", line 12, in get_content
content=texts.text.strip().split('\xa0'*4)
AttributeError: 'NoneType' object has no attribute 'text'
原因分析:
分析:
target='https://www.xsbiquge.com/15_15338/'
req=requests.get(url=target)
req.encoding='utf-8'
html=req.text
chapter_bs=BeautifulSoup(html,'lxml')
chapters=chapter_bs.find('div',id='content')
id写错导致没有找到text
解决方案:
将content改为list即可。要明确爬取的标签
在Python爬虫项目中,遇到AttributeError: 'NoneType' object has no attribute 'text'的问题,通常是因为尝试访问不存在的元素text属性。原因可能是ID选择错误,导致无法找到预期的元素。为了解决这个问题,需要检查并修正选择器,确保它能正确匹配到目标元素。一种解决方案是将content转换为list类型,确保在遍历时能处理可能为空的情况。
2万+





