scrapy案例——当当网的爬取一

项目名称:当当网的爬取一------爬取青春文学的书籍数据

案例需求:

1.使用scrapy爬虫技术爬取当当网中青春文学的书籍数据,包括(标题、现价、定价、作者、出版日期、出版社、书本详情和书本图片url)

2.将获取到的数据保存在数据库中

3.实现分页爬取

分析

1.数据包的获取

2.准备工作:

复制代码
# ROBOTSTXT_OBEY = True
复制代码
ITEM_PIPELINES = {
   "Dangd.pipelines.DangdPipeline": 300,
}
复制代码
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"

3.解析数据

同理

复制代码
标题:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li[3]/p[1]/a
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li[4]/p[1]/a

/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[1]/a

现价:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[3]/span[1]
定价:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[3]/span[2]

作者:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[5]/span[1]/a[1]

出版日期:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[5]/span[2]

出版社:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[5]/span[3]/a

书本详情:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[2]/text()

图片:
/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/a/img
复制代码
title=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/a/img/@alt').extract()
        price_xz=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[3]/span[1]/text()').extract()
        price_dj=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[3]/span[2]/text()').extract()
        author=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[5]/span[1]/a[1]/text()').extract()
        date=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[5]/span[2]/text()').extract()
        cbs=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[5]/span[3]/a/text()').extract()
        detail=response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/p[2]/text()').extract()
        # url='http://'+response.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li/a/img/@src').extract()
        url=response.xpath('//ul[@id="component_59"]/li')
        for t,px,pd,a,d,c,de,u in zip(title,price_xz,price_dj,author,date,cbs,detail,url):
            src = u.xpath('.//img/@data-original').extract_first()
            if src:
                src='http:'+src
            else:
                src = 'http:'+u.xpath('.//img/@src').extract_first()

            # print('标题:',title)
            # print('现价:',price_xz)
            # print('定价:',price_dj)
            # print('作者:',author)
            # print('出版日期:',date)
            # print('出版社:',cbs)
            # print('书本详情:',detail)
            # print('书本图片:',url)
            print('标题:', t)
            print('现价:', px)
            print('定价:', pd)
            print('作者:', a)
            print('出版日期:', d.replace('/',''))
            print('出版社:', c)
            print('书本详情:', de)
            print('书本图片:', src)
            print('=====================================')

items.py

复制代码
class DangdItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()#标题
    price_xz = scrapy.Field()#现价
    price_dj = scrapy.Field()#定价
    author = scrapy.Field()#作者
    date = scrapy.Field()#出版日期
    cbs = scrapy.Field()#出版社
    detail = scrapy.Field()#书本详情
    url = scrapy.Field()#书本图片

4.翻页

第一页 http://category.dangdang.com/cp01.01.00.00.00.00.html

第二页 http://category.dangdang.com/pg2-cp01.01.00.00.00.00.html

第三页 http://category.dangdang.com/pg3-cp01.01.00.00.00.00.html

第四页 http://category.dangdang.com/pg4-cp01.01.00.00.00.00.html

所以总结:

http://category.dangdang.com/pg{}-cp01.01.00.00.00.00.html

复制代码
        if self.page<10:#就不保存完了
            self.page=self.page+1
            url=self.base_url+str(self.page)+'-cp01.01.00.00.00.00.html'
            print('+++++++++++++++++第{}页++++++++++++++++++'.format(self.page))
            yield scrapy.Request(url=url,callback=self.parse)

5.保存至数据库

复制代码
class DangdPipeline:
    def __init__(self):
        # 打开文件
        # 连接数据库
        self.conn = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            passwd='wx990826',
            db='dangdang',
        )
        self.cur = self.conn.cursor()

    def process_item(self, item, spider):
        sqli = "insert into qcwx(title,price_xz,price_dj,author,date,cbs,detail,url) values(%s,%s,%s,%s,%s,%s,%s,%s)"

        self.cur.execute(sqli, (
            item['title'], item['price_xz'], item['price_dj'], item['author'], item['date'], item['cbs'], item['detail'], item['url']))
        self.conn.commit()
        print('保存完毕')
        return item

6.运行

复制代码
from scrapy import cmdline
cmdline.execute(['scrapy','crawl','dangd','--nolog'])

运行结果:

注意:该网站的图片为懒加载

相关推荐
喵手6 小时前
Python爬虫实战:采集菜谱网站的“分类/列表页”(例如“家常菜”或“烘焙”频道)数据,构建高可用的美食菜谱数据采集流水线(附CSV导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集菜谱网站数据·家常菜或烘焙频道·构建高可用食谱数据采集系统
喵手6 小时前
Python爬虫实战:硬核解析 Google Chrome 官方更新日志(正则+文本清洗篇)(附 CSV 导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·监控谷歌版本发布历史·获取稳定版更新日志
青春不朽51218 小时前
Scrapy框架入门指南
python·scrapy
深蓝电商API20 小时前
处理字体反爬:woff字体文件解析实战
爬虫·python
NPE~21 小时前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
喵手1 天前
Python爬虫实战:电商价格监控系统 - 从定时任务到历史趋势分析的完整实战(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·电商价格监控系统·从定时任务到历史趋势分析·采集结果sqlite存储
摘星|1 天前
正则匹配与爬虫爬取图片路径综合练习
爬虫
喵手1 天前
Python爬虫实战:京东/淘宝搜索多页爬虫实战 - 从反爬对抗到数据入库的完整工程化方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·京东淘宝页面数据采集·反爬对抗到数据入库·采集结果csv导出
0思必得01 天前
[Web自动化] Selenium获取元素的子元素
前端·爬虫·selenium·自动化·web自动化
搂着猫睡的小鱼鱼2 天前
Ozon 商品页数据解析与提取 API
爬虫·php