1、目的
将豆瓣读书top250排名保存到本地excel,包括书名,作者,评分,评论数,简评,网址。用到了requests,res,BeautifulSoup,csv库。
2、分析网址
打开豆瓣读书网址:https://book.douban.com/top250
第一页:https://book.douban.com/top250
第二页:https://book.douban.com/top250?start=25
第三页:https://book.douban.com/top250?start=50

第十页:https://book.douban.com/top250?start=225
把第一页的网址改为:https://book.douban.com/top250?start=0
找到规律,通过修改最后的数字改变网址,先定义一个函数获取所有网址,并存入列表。

def get_all_url():      #定义获取所有网址的函数urls = []for i in range(0, 250, 25):url_1 = 'https://book.douban.com/top250?start1={}'.format(i)urls.append(url_1)return urls

返回urls列表

3、分析网站内容
打开任一网址,右击书名–检查

打开后显示:

通过观察其他书名,可以通过div class = pl2定位。
先导入需要的库文件,使用requests获取网页内容,使用beautifulsoup解析网页。header要写,模拟为浏览器,不然可能返回空值。

import requests   #导入requests库,用于获取网页数据
import re         #导入re库,用于正则表达式筛选数据
from bs4 import BeautifulSoup  #导入库,用于解析网页
import csv        #导入库,用于创建csv文件并写入数据url_2 = 'https://book.douban.com/top250?start=50'
header = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like\Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0\.3 Mobile/15E148 Safari/604.1'}
res = requests.get(url_2, headers=header)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'lxml')data_name = soup.find_all('div', class_='pl2')[1]
names = data_name.a.get('title')
href = data_name.a.get('href')

打印测试结果,返回书名和网址:

相同的方法,分别定位到作者,评分,

data_author = soup.find_all('p', class_='pl')[1]
authors = data_author.get_text().split('/')[0]data_score = soup.find_all('span', class_='rating_nums')[1]
scores = data_score.get_text()data_msg = soup.find_all('span', class_='pl')[1].get_text()
msgs = re.findall('\d+', data_msg)[0]

打印结果测试:

定位简评稍微复杂,因为如果直接用定位到的路径,会发现有的书名和简评不对应,原因就是有的书没有简评,所以导致错位,解决办法就是通过简评的上一级进行定位,然后再查找,如果没有简评的会返回None值,然后再进行一次判断。

通过两次定位

data_com = soup.find_all('td', valign='top')[3].find('span', class_='inq')if data_com is not None:comments = data_com.get_text()
else:comments = '无'

打印测试结果:

至此,所有的结果都已经得到,下面要将本书的信息写入到本地的csv文件中。使用到了csv库。

with open('c:/豆瓣读书TOP250.csv', 'w', newline='', encoding='utf-8-sig') as file:book_info = csv.writer(file)book_info.writerow(('序号', '书名', '作者', '评分', '评论数', '简评', '网址'))book_info.writerow((1, names, authors, scores, msgs, comments, href,))

运行后,可看到C盘多了一个文件‘豆瓣读书TOP250.csv’,打开后

成功写入了。
注意的地方:
open(‘c:/豆瓣读书TOP250.csv’, ‘w’, newline=’’, encoding=‘utf-8-sig’)
如果不写newline=’ ',最后的结果是每条数据后面都有一个空白行。
如果只写encoding=‘utf-8’,会导致打开csv文件时出现乱码,此时如果用文本文档打开再保存也可以解决乱码问题。直接写’utf-8-sig’则直接打开即可正常显示。

单本书的信息已经成功读取并写入,接下来考虑的是读取每页的书的信息。每页有25本书,所以通过以下语句实现。

    for i in range(25):data_name = soup.find_all('div', class_='pl2')[i]names = data_name.a.get('title')href = data_name.a.get('href')data_author = soup.find_all('p', class_='pl')[i]authors = data_author.get_text().split('/')[0]data_score = soup.find_all('span', class_='rating_nums')[i]scores = data_score.get_text()data_msg = soup.find_all('span', class_='pl')[i].get_text()msgs = re.findall('\d+', data_msg)[0]data_com = soup.find_all('td', valign='top')[2 * i + 1].find('span', class_='inq')if data_com is not None:comments = data_com.get_text()else:comments = '无'book_info.writerow((1, names, authors, scores, msgs, comments, href))

查看结果,本页的数据全部存入excel,序号那一列暂时写入1,在总程序里会再做处理。

接下来就是要把所有页面的数据爬取下来,文章的开头我们已经把所有的网址存入了一个列表中,所以只要把列表中的网址每次拿出一个,执行一遍上面的程序,等所有网页执行完了,我们就得到了所有的数据了。
所以我构建了一个get_book_info(url)的函数,url代表要爬取的网址,只要每次改变url的网址,就可以将数据写入csv文件。

def get_book_info(url_2):   #定义函数,通过网址参数获取整页所需数据,并将其存入csv文件global ids              #定义序号变量header = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like\Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0\.3 Mobile/15E148 Safari/604.1'}res = requests.get(url_2, headers=header)res.encoding = 'utf-8'soup = BeautifulSoup(res.text, 'lxml')for i in range(25):data_name = soup.find_all('div', class_='pl2')[i]names = data_name.a.get('title')href = data_name.a.get('href')data_author = soup.find_all('p', class_='pl')[i]authors = data_author.get_text().split('/')[0]data_score = soup.find_all('span', class_='rating_nums')[i]scores = data_score.get_text()data_msg = soup.find_all('span', class_='pl')[i].get_text()msgs = re.findall('\d+', data_msg)[0]data_com = soup.find_all('td', valign='top')[2 * i + 1].find('span', class_='inq')if data_com is not None:comments = data_com.get_text()else:comments = '无'ids += 1book_info.writerow((ids, names, authors, scores, msgs, comments, href,))

获取网址和获取每个网址数据的函数都定义好了,接下来需要通过主程序调用他们。

if __name__ == '__main__':   #主函数try:with open('c:/豆瓣读书TOP250.csv', 'w', newline='', encoding='utf-8-sig') as file:book_info = csv.writer(file)book_info.writerow(('序号', '书名', '作者', '评分', '评论数', '简评', '网址'))url_list = get_all_url()ids = 0for url in url_list:get_book_info(url)except Exception as e:  #异常处理print('Error:', e)
print('下载完成!')

其中使用try语句,增加了异常处理,如果异常可以打印出异常信息。使用with open语句目的是打开后可以确保文件能自动关闭。

全部代码如下:
最开头的注释#coding=gbk,是因为编码问题当用命令行调用此py文件时报如下错误:SyntaxError: Non-UTF-8 code starting with ‘\xb5’ in file 1111.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
如果直接用pycharm运行则无此问题。

#coding=gbk
import requests   #导入requests库,用于获取网页数据
import re         #导入re库,用于正则表达式筛选数据
from bs4 import BeautifulSoup  #导入库,用于解析网页
import csv        #导入库,用于创建csv文件并写入数据def get_all_url():      #定义获取所有网址的函数urls = []for i in range(0, 250, 25):url_1 = 'https://book.douban.com/top250?start={}'.format(i)urls.append(url_1)return urlsdef get_book_info(url_2):   #定义函数,通过网址参数获取整页所需数据,并将其存入csv文件global ids              #定义序号变量header = {'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like\Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0\.3 Mobile/15E148 Safari/604.1'}res = requests.get(url_2, headers=header)res.encoding = 'utf-8'soup = BeautifulSoup(res.text, 'lxml')for i in range(25):data_name = soup.find_all('div', class_='pl2')[i]names = data_name.a.get('title')href = data_name.a.get('href')data_author = soup.find_all('p', class_='pl')[i]authors = data_author.get_text().split('/')[0]data_score = soup.find_all('span', class_='rating_nums')[i]scores = data_score.get_text()data_msg = soup.find_all('span', class_='pl')[i].get_text()msgs = re.findall('\d+', data_msg)[0]data_com = soup.find_all('td', valign='top')[2 * i + 1].find('span', class_='inq')if data_com is not None:comments = data_com.get_text()else:comments = '无'ids += 1book_info.writerow((ids, names, authors, scores, msgs, comments, href,))if __name__ == '__main__':   #主函数try:with open('c:/豆瓣读书TOP250.csv', 'w', newline='', encoding='utf-8-sig') as file:book_info = csv.writer(file)book_info.writerow(('序号', '书名', '作者', '评分', '评论数', '简评', '网址'))url_list = get_all_url()ids = 0for url in url_list:get_book_info(url)except Exception as e:  #异常时执行print('Error:', e)
print('下载完成!')

爬虫豆瓣读书top250,保存为本地csv文件,可用excel查看(具体步骤和容易遇到的坑)相关推荐

  1. 爬虫豆瓣读书top250,保存为本地csv文件

    爬虫豆瓣读书top250,保存为本地csv文件 目的 将豆瓣读书top250排名保存到本地excel,包括书名,作者,评分,评论数,简评,网址.用到了requests,res,BeautifulSou ...

  2. 爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中

    爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中 获取源代码请访问https://github.com/zhang020801/douban_bookTop250 一.程序源代码 ...

  3. python爬虫豆瓣读书top250+数据清洗+数据库+Java后端开发+Echarts数据可视化(一)

    由于刚上完了商业智能实训的课程,根据老师的要求我们做了一个完整的项目. 1. 项目要求与内容 项目具体要求:利用python爬取数据并进行清洗和预处理,将清洗后的数据存到数据库中,后端利用Java或是 ...

  4. python爬虫豆瓣读书top250+数据清洗+数据库+Java后端开发+Echarts数据可视化(二)

    之前的博客已经写了python爬取豆瓣读书top250的相关信息,接下来继续看如何清洗数据. 如果有没看懂的或是不了解上一部分说的是什么内容的,请看https://blog.csdn.net/qq_4 ...

  5. python爬虫豆瓣读书top250+数据清洗+数据库+Java后端开发+Echarts数据可视化(四)

    之前的博客已经写了python爬取豆瓣读书top250的相关信息和清洗数据.将数据导入数据库并创建相应的数据表,以及进行项目准备工作,接下来开始正式编写后台代码. 如果有没看懂的或是不了解上一部分说的 ...

  6. Python爬虫实战之一 - 基于Requests爬取拉勾网招聘信息,并保存至本地csv文件

    Python爬虫实战之二 - 基于Requests抓取拉勾网招聘信息 ---------------readme--------------- 简介:本人产品汪一枚,Python自学数月,对于小白,本 ...

  7. 爬取豆瓣音乐TOP250数据保存到csv文件和xls文件

    爬取的目标网址:https://music.douban.com/top250 利用lxml库,获取前10页的信息,需要爬取的信息包括歌曲名.表演者.流派.发行时间.评分和评论人数,把这些信息存到cs ...

  8. [爬虫系列(二)]爬取豆瓣读书Top250,并保存每本书

    这里我们要爬起豆瓣读书Top250,并保存每本书的书名,信息,简要介绍和作者信息.  这里,仍然分为三步:  1.url分析  2.数据分析  3.爬取数据 1.url分析 豆瓣读书Top250的ur ...

  9. Java网络爬虫--一步步使用Java网络爬虫技术实现豆瓣读书Top250数据的爬取,并插入数据库

    一步步使用Java网络爬虫技术实现豆瓣读书Top250数据的爬取,并插入数据库 目录 一步步使用Java网络爬虫技术实现豆瓣读书Top250数据的爬取,并插入数据库 第一步:创建项目,搭建项目结构 p ...

最新文章

  1. 一文看懂 AI 训练集、验证集、测试集(附:分割方法+交叉验证)
  2. PHP时间戳 strtotime()使用方法和技巧
  3. 到底该不该上马Vista 中小企业升级全攻略(上)
  4. OCS Inventory NG使用之win平台下的AGENT端安装与信息收集(一)
  5. 凯西·奥尼尔:盲目信仰大数据的时代必须结束 | 算法密码
  6. 什么是IP地址、IP协议?
  7. 【车载】FMEA、FTA、FMEDA
  8. linux 离线迅雷下载软件,Linux 迅雷离线客户端!!!!!!!!!!!!
  9. 【NOIP practice】BSOJ 3140 冲出亚洲 模拟
  10. 服务器中java项目调用Kettle转换脚本ktr
  11. configure error:Package requirements (openssl) were not met
  12. 行人重识别 代码阅读(来自郑哲东 简单行人重识别代码到88%准确率)
  13. 数据库中的数据完整性约束
  14. 恶意网站可利用浏览器扩展 API,窃取浏览器数据
  15. cocos creator教程【打造路径编辑系统】
  16. 计算机vf中rest是什么意思,vf常用命令(全)
  17. 清屏函数 mysql_c++清屏函数是什么
  18. day72 JavaWeb框架阶段——RabbitMQ消息队列【了解常见的MQ产品,了解RabbitMQ的5种消息模型,会使用Spring AMQP】
  19. 如何用PDF阅读器实现PDF旋转
  20. 物理CPU CPU核数 逻辑CPU 几核几线程的概念详解

热门文章

  1. 公司不让使用XShell破解版后,我准备用这种方式!
  2. 流行的几种世界观来源
  3. 谷歌Chrome浏览器无法安装插件的解决方法
  4. Android RadioGroup中横向 竖向布局RadioButton的问题
  5. c语言三角分解法解方程,用直接三角分解法解线性方程组.ppt
  6. matlab如何绘制相关系数热力图,相关系数矩阵与热力图heatmap
  7. 哈萨克斯坦大型“零元购”抓捕现场
  8. 联想小新13pro锐龙版网卡_联想小新Pro13 锐龙版简测
  9. cocos creator 插件开发
  10. 如何将电子签名透明化处理