当前位置: 首页 > news >正文

Python爬虫——scrapy_当当网图书管道封装

  1. 创建爬虫项目
srcapy startproject scrapy_dangdang
  1. 进入到spider文件里创建爬虫文件(这里爬取的是青春文学,仙侠玄幻分类)
srcapy genspider dang http://category.dangdang.com/cp01.01.07.00.00.00.html
  1. 获取图片、名字和价格
# 所有的seletor的对象,都可以再次调用xpath方法
li_list = response.xpath('//div[@id="search_nature_rg"]//li')for li in li_list:# 获取图片src = li.xpath('.//img/@data-original').extract_first()# 第一张图片和其他图片的标签的属性不一样# 第一张图片的src是可以使用的,其他图片的地址在data-original里if src:src = srcelse:src = li.xpath('.//img/@src').extract_first()# 获取名字name = li.xpath('.//img/@alt').extract_first()# 获取价格price = li.xpath('.//p[@class="price"]/span[1]/text()').extract_first()print(src, name, price)
  1. 在items里定义要下载的数据
import scrapyclass ScrapyDangdang39Item(scrapy.Item):# 要下载的数据都有什么# 图片src = scrapy.Field()# 名字name = scrapy.Field()# 价格price = scrapy.Field()
  1. 在dang.py里导入items
from ..items import ScrapyDangdang39Item
  1. 在parse方法里定义一个对象book,然后把获取到的值传递到pipelines
book = ScrapyDangdang39Item(src=src, name=name, price=price)# 获取一个book就将book传递给pipelines
yield book
  1. 开启管道
    在settings中,把这几行代码取消注释
    在这里插入图片描述
    管道可以有很多个,但是管道是有优先级的,优先级的范围是1到1000 值越小,优先级越高
  2. 下载数据
    打开piplines.py
class ScrapyDangdang39Pipeline:# 方法1# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp = open('book.json', 'w', encoding='utf-8')def process_item(self, item, spider):# item就是yield后面的book对象# 1.write方法必须要写一个字符串,而不是其他的对象# 2.w模式,每一个对象都会打开一次文件,然后覆盖之前的内容,所以使用a模式with open('book.json', 'a', encoding='utf-8')as fp:fp.write(str(item))return item

但是这种模式不推荐,因为每传递过来一个数据,就要打开一次文件,对文件的操作太过频繁
换一种方法

class ScrapyDangdang39Pipeline:# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp = open('book.json', 'w', encoding='utf-8')def process_item(self, item, spider):# item就是yield后面的book对象self.fp.write(str(item))return item# 在爬虫文件执行完后执行的一个方法def close_spider(self, spider):self.fp.close()
  1. 运行dang.py文件就可以把数据保存到本地了

完整代码
dang.py

import scrapy
from ..items import ScrapyDangdang39Itemclass DangSpider(scrapy.Spider):name = "dang"allowed_domains = ["category.dangdang.com"]start_urls = ["http://category.dangdang.com/cp01.01.07.00.00.00.html"]def parse(self, response):# 所有的seletor的对象,都可以再次调用xpath方法li_list = response.xpath('//div[@id="search_nature_rg"]//li')for li in li_list:# 获取图片src = li.xpath('.//img/@data-original').extract_first()# 第一张图片和其他图片的标签的属性不一样# 第一张图片的src是可以使用的,其他图片的地址在data-original里if src:src = srcelse:src = li.xpath('.//img/@src').extract_first()# 获取名字name = li.xpath('.//img/@alt').extract_first()# 获取价格price = li.xpath('.//p[@class="price"]/span[1]/text()').extract_first()book = ScrapyDangdang39Item(src=src, name=name, price=price)# 获取一个book就将book传递给pipelinesyield book

items.py

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass ScrapyDangdang39Item(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()# 要下载的数据都有什么# 图片src = scrapy.Field()# 名字name = scrapy.Field()# 价格price = scrapy.Field()

pipelines.py

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html# useful for handling different item types with a single interface
from itemadapter import ItemAdapter# 如果想使用管道,就必须在settings中开启管道
class ScrapyDangdang39Pipeline:# 方法1# 在爬虫文件执行前执行的一个方法def open_spider(self, spider):self.fp = open('book.json', 'w', encoding='utf-8')def process_item(self, item, spider):# item就是yield后面的book对象# 这种模式不推荐# with open('book.json', 'a', encoding='utf-8')as fp:#     fp.write(str(item))self.fp.write(str(item))return item# 在爬虫文件执行完后执行的一个方法def close_spider(self, spider):self.fp.close()
http://www.lryc.cn/news/127539.html

相关文章:

  • Linux下如何修改CPU 电源工作模式
  • Effective C++学习笔记(8)
  • 学校如何公布录取情况表?这个不用技术的方法,小白老师都能轻松制作
  • Chart GPT免费可用地址共享资源
  • 设计模式十八:中介者模式(Mediator Pattern)
  • 神经网络基础-神经网络补充概念-12-向量化逻辑回归的梯度输出
  • 2023-08-16力扣每日一题
  • 耗资170亿美元?三星电子在得克萨斯州建设新的半导体工厂
  • 黑马项目一阶段面试58题 Web14题(一)
  • 多线程与高并发--------线程池
  • 深度学习实战48-【未来的专家团队】基于AutoCompany模型的自动化企业概念设计与设想
  • 深入剖析:如何通过API优化云计算架构?快来看!
  • 基于STM32设计的中药分装系统
  • 消息队列学习笔记
  • 贝锐蒲公英:助力企业打造稳定高效的智能安防监控网络
  • SASS 学习笔记
  • Web菜鸟教程 - Springboot接入认证授权模块
  • 【深入理解ES6】块级作用域绑定
  • 使用fake为数据库生成随机数据
  • 树结构转List
  • Android复习(Android基础-四大组件)——Broadcast
  • Ubuntu下mysql8开启远程连接
  • java对象和json类型转换
  • elasticsearch-head 插件
  • Neo4j之FOREACH基础
  • 【SpringBoot】| 接口架构风格—RESTful
  • CentOS系统环境搭建(十)——CentOS7定时任务
  • 如何在安卓设备上安装并使用 ONLYOFFICE 文档
  • 【制作npm包1】申请npm账号、认识个人包和组织包
  • linux学习(文件描述符)[11]