当前位置:   article > 正文

爬虫练习-爬取豆瓣网图书TOP250的数据

爬虫练习-爬取豆瓣网图书TOP250的数据

前言

爬取豆瓣网图书TOP250的数据,书名、链接、作者、出版社、出版时间、价格、评分、评语,并将数据存储于CSV文件中

本文为整理代码,梳理思路,验证代码有效性——2019.12.15


环境:
Python3(Anaconda3)
PyCharm
Chrome浏览器

主要模块:
lxml
requests
csv

1.

爬取的豆瓣图书首页如下
在这里插入图片描述

2.

分析URL规律

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=75  # 第四页
...
  • 1
  • 2
  • 3
  • 4
  • 5

可以发现首页的URL与其他的URL格式不一样,但是通过测试发现可以通过URLhttps://book.douban.com/top250?start=0来访问首页
我们用列表解析式来构造出相应的URL列表

urls = ['https://book.douban.com/top250?start={}'.format(str(i)) for i in range(0,250,25)]
  • 1

3.

爬取书名、链接、作者、出版社、出版时间、价格、评分、评语等数据
在这里插入图片描述
分析源码,进行解析
在这里插入图片描述
利用Xpath对其解析

# 所有信息均在tr class="item"中,先将该模块提取出来方便进一步解析
infos = selector.xpath('//tr[@class="item"]')

for info in infos:
     name = info.xpath('td/div/a/@title')[0]  # 书名
     url = info.xpath('td/div/a/@href')[0]  # 链接
     book_infos = info.xpath('td/p/text()')[0]   
     author = book_infos.split('/')[0]  # 作者
     publisher = book_infos.split('/')[-3]  # 出版社
     date = book_infos.split('/')[-2]  # 出版时间
     price = book_infos.split('/')[-1]  # 价格
     rate = info.xpath('td/div/span[2]/text()')[0]  # 评分
     comments = info.xpath('td/p/span/text()')  # 评语
     comment = comments[0] if len(comments) != 0 else "空"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.

将数据存储与CSV文件中
存储过程比较简单,“将大象装进冰箱”三步

  1. “打开冰箱”
# 创建csv
fp = open('doubanbook.csv', 'wt', newline='', encoding='utf-8')
  • 1
  • 2
  1. “将大象装进去”
# 写入数据
writer.writerow((name, url, author, publisher, date, price, rate,comment))
  • 1
  • 2
  1. “关上冰箱”
# 关闭csv文件
fp.close()
  • 1
  • 2

至此,爬取豆瓣网图书TOP250的数据就结束了


A.完整代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入相应的库文件
from lxml import etree
import requests
import csv

# 创建csv
fp = open('doubanbook.csv', 'wt', newline='', encoding='utf-8')

# 写入header
writer = csv.writer(fp)
writer.writerow(('name', 'url',  'author', 'publisher', 'date', 'price', 'rate', 'comment'))

# 构造urls
urls = ['https://book.douban.com/top250? start={}'.format(str(i)) for i in range(0,250,25)]

# 加入请求头
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36'
                 '(KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}

for url in urls:
    html = requests.get(url, headers=headers)
    selector = etree.HTML(html.text)
    # 取大标签,以此循环
    infos = selector.xpath('//tr[@class="item"]')

    for info in infos:
        name = info.xpath('td/div/a/@title')[0]  # 书名
        url = info.xpath('td/div/a/@href')[0]  # 链接
        book_infos = info.xpath('td/p/text()')[0]   
        author = book_infos.split('/')[0]  # 作者
        publisher = book_infos.split('/')[-3]  # 出版社
        date = book_infos.split('/')[-2]  # 出版时间
        price = book_infos.split('/')[-1]  # 价格
        rate = info.xpath('td/div/span[2]/text()')[0]  # 评分
        comments = info.xpath('td/p/span/text()')  # 评语
        comment = comments[0] if len(comments) != 0 else "空"
        # 写入数据
        writer.writerow((name, url, author, publisher, date, price, rate,comment))

# 关闭csv文件
fp.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/401413
推荐阅读
相关标签
  

闽ICP备14008679号