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

项目场景:

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,python,爬虫)