天天看点

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

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

一、创建并连接mysql数据库

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

C:\Users\Administrator>cd /d C:\Windows\System32
C:\Windows\System32>net start mysql80
           
python基于scrapy框架爬取当当图书信息

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

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

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

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

二、创建scrapy爬虫工程

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

三、爬取需要的页面信息

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

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

python基于scrapy框架爬取当当图书信息
name = 'book_info'
  allowed_domains = ['dangdang.com']
  start_urls = ['http://category.dangdang.com/cp01.54.17.00.00.00.html']
           

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

python基于scrapy框架爬取当当图书信息
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())
           
python基于scrapy框架爬取当当图书信息

网页上的图书信息已经爬取下来了,可以在workbench中打开查看

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

扫描下方二维码,关注“海海读书”公众号,回复“当当”二字,即可获得程序原代码

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