AttributeError: 'NoneType' object has no attribute 'find_all'

weixin_45844534 2019-11-10 09:19:16
源代码如下:
import requests
from bs4 import BeautifulSoup

# 请求数据
url = 'https://www.douban.com/latest'
headers = {'User-Agent': "Mozilla/5.0 (x11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0"}
data = requests.get(url, headers=headers)
# print(data.text)

# 解析数据
soup = BeautifulSoup(data.text, 'lxml')
# print(soup)

# 观察到网页上的书籍按左右两边分布,按照标签分别提取
books_left = soup.find('ul', {'class': 'cover-col-4 clearfix'})
books_left = books_left.find_all('li')
books_right = soup.find('ul', {'class': 'cover-col-4 pl20 clearfix'})
books_right = books_right.find_all('li')
books = list(books_left) + list(books_right)
# 对每一个图书区块进行相同操作,获取图书信息
img_urls = []
titles = []
ratings = []
authors = []
details = []
for book in books:
# 图书封面图片URL地址
img_url = book.find_all('a')[0].find('img').get('src')
img_url.append(img_url)
# 图书标题
title = book.find_all('a')[1].get_text()
titles.append(title)
# print(title)

# 评价星级
rating = book.find('p', {'class': 'rating'}).get_text()
rating = rating.replace('\n', '').replace(' ', '')
rating.append(rating)

# 作者及出版信息
author = book.find('p', {'class': 'color-gray'}).get_text()
author = author.replace('\n', '').replace(' ', '')
authors.append(author)

# 图书简介
detail = book.find_all('p')[2].get_text()
detail = detail.replace('\n', '').replace(' ', '')
authors.append(detail)
print("img_urls:", img_urls)
print("titles:", titles)
print("rating:", ratings)
print("authorS:", authors)
print("details:", details)

报错:
Traceback (most recent call last):
File "C:/Users/阙江俊/PycharmProjects/untitled1/test5.py", line 16, in <module>
books_left = books_left.find_all('li')
AttributeError: 'NoneType' object has no attribute 'find_all'

求大神帮忙解答一下
...全文
2090 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧