python基于scrapy框架爬取当当图书信息

本次爬取主要任务是用scrapy框架爬取当当图书名称、作者是、价格、出版社、图片等信息,为了方便后期的处理和应用,可以将信息利用mysql数据库存储。

一、创建并连接mysql数据库

1、利用cmd命令进入Windows系统,并启动mysql数据库

C:\Users\Administrator>cd /d C:\Windows\System32
C:\Windows\System32>net start mysql80

显示服务已经启动成功

2、利用MySQL Workbench工具创建数据库并新建一个Table列表用来存储数据

book_info_list为新建的列表

3、编辑表格,添加想获取的信息名称

主要是获取图书名称、链接、价格、作者、出版社、IS

二、创建scrapy爬虫工程

在这里插入图片描述

三、爬取需要的页面信息

以自取当当网中的【地理信息管理系统】这个类目为例:

1、获取初始页面的网址,也就是start_urls的值

在这里插入图片描述

  name = 'book_info'
  allowed_domains = ['dangdang.com']
  start_urls = ['http://category.dangdang.com/cp01.54.17.00.00.00.html']

2、用xpath解析当前网页,查找图书信息所在的标签树

在这里插入图片描述

info_list = response.xpath('//ul[@class="bigimg"]/li')

3、在标签树下提取需要的信息

 for li in info_list:
        item = DangdangspiderItem()
         item['title'] = li.xpath('./a/@title').get()
         item['link'] = li.xpath('./a/@href').get()
         item['comment'] = li.xpath('.//a[@class="search_comment_num"]/text()').get()
         price = li.xpath('.//span[@class="search_now_price"]/text()').get()
         item['price'] = price.strip('¥')
         public_info = li.xpath('.//p[@class="search_book_author"]//text()').getall()
         public_info = ','.join(public_info)
         item['author'] = public_info.split('/')[0].strip().strip(',')
         item['public_time'] = public_info.split('/')[1].strip().strip(',')
         item['publictor'] = public_info.split('/')[2].strip().strip(',')
         item['detail'] = li.xpath('./p[@class="detail"]/text()').get()
         yield item

4、在pipelines中将数据保存在mysql数据库中

def process_item(self, item, spider):
    title = item['title']
    print("正在处理%s的信息" % title)
    link = item['link']
    comment = item['comment']
    price = item['price']
    detail = item['detail']
    author = item['author']
    public_time = item['public_time']
    publictor = item['publictor']
    isbn, img_url_list = self.parse_single_book(link)
    book_dir_path = self.get_img(img_url_list, title)
    isbn_img_path = book_dir_path + '\isbn_img'
    barcode.generate('code128', code=isbn, writer=ImageWriter(), output=isbn_img_path)
    data = (title, price, author, comment, public_time, isbn, link, publictor, detail)
    connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='dangdang',
                                 charset='utf8mb4')
    cursor = connection.cursor()
    sql = "insert into book_info_list(Title,Price,Author, Comment, Public_time, ISBN, Link,Publictor,Detail) values (%s, %s, %s, %s, %s, %s, %s, %s, %s);"
    try:
        print('正在保存数据')
        cursor.execute(sql, data)
        connection.commit()
        print('保存成功')
    except Exception as err:
        print(err)
    cursor.close()
    connection.close()
    return item

4、设置翻页

本次只提取前3页的信息

for i in range(2, 4):
    url = 'http://category.dangdang.com/pg' + str(i) + '-cp01.54.02.00.00.00.html'
    yield Request(url, callback=self.parse)

5、新建start.py文件用以运行工程

from scrapy import cmdline
cmdline.execute('scrapy crawl book_info'.split())

在这里插入图片描述
网页上的图书信息已经爬取下来了,可以在workbench中打开查看
在这里插入图片描述
扫描下方二维码,关注“海海读书”公众号,回复“当当”二字,即可获得程序原代码
在这里插入图片描述

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值