python爬虫出现AttributeError: ‘NoneType‘ object has no attribute ‘text‘错误

给小伙伴们推荐一个Python学习裙,热烈欢迎各位小伙伴的到来,大家在学习Python的过程中遇到了什么问题都可以一起探讨,不要在里面发广告哦!

610380249

项目场景:

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即可。要明确爬取的标签

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容