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

尚硅谷爬虫note15

一、当当网

1. 保存数据

        数据交给pipelines保存

items中的类名:  DemoNddwItem

class DemoNddwItem(scrapy.Item):

        变量名 = 类名()

            book =  DemoNddwItem(src = src, name = name, price = price)

导入:

        from 项目名.items import 类名

from demo_nddw.items import NddwSpider

2. yield

        return一个返回值

yield book:

        获取一个book,就将book交给pipelines

3. pipelines(管道):

        想使用管道,必须在settings中开启管道:
如何开启——》解除管道的注释即可开启管道

 管道可以有很多个

管道是有优先级的:

        优先级范围:1~1000

        值越小,优先级越高

 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 ItemAdapterclass DemoNddwPipeline:
#爬虫文件开始前执行的方法def open_spider(self, spider)self.fp('book.json', 'w', encoding='utf-8')# items:yield后面的book对象def process_item(self, item, spider):
# #以下模式不推荐:因为每传进一个对象,就打开一次文件,对文件的操作过于频繁
# #2个方法
# #1)爬虫文件开始前执行的方法
# def open_spider(self,spider)
#     self.fp('book.json','w',encoding = 'utf-8')
# #2)爬虫文件执行完成后执行的方法
# def close_spider(self,spider)
#     self.fp.close()
# # 中间
#     self.fp.write(str(item))#将数据保存到文件中# (1)write方法必须是一个字符串强制转换:fp.write(item)——》fp.write(str(item))# (2)w模式会每一个对象都打开一次文件,后面打开的会覆盖前面打开的,然后关闭:将w模式改为a(追加)模式:解决:‘w’——》‘a’#不推荐的模式# 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()

 不推荐的模式:

# #以下模式不推荐:因为每传进一个对象,就打开一次文件,对文件的操作过于频繁#将数据保存到文件中# (1)write方法必须是一个字符串强制转换:fp.write(item)——》fp.write(str(item))# (2)w模式会每一个对象都打开一次文件,后面打开的会覆盖前面打开的,然后关闭:将w模式改为a(追加)模式:解决:‘w’——》‘a’#不推荐的模式# with open('book.json','a',encoding='utf-8') as fp:#     fp.write(str(item))


2个方法:

# #2个方法
# #1)爬虫文件开始前执行的方法
# def open_spider(self,spider)
#     self.fp('book.json','w',encoding = 'utf-8')
# #2)爬虫文件执行完成后执行的方法
# def close_spider(self,spider)
#     self.fp.close()
# # 中间
#     self.fp.write(str(item))

      复制items中的类名:DemoNddwItem

导入ddw中,

from demo_nddw.items import DemoNddwItem

并在ddw中使用

book = DemoNddwItem(src = src, name = name, price = price)

ddw.py

import scrapy
from demo_nddw.items import DemoNddwItemclass NddwSpider(scrapy.Spider):name = "nddw"allowed_domains = ["category.dangdang.com"]start_urls = ["https://category.dangdang.com/cp01.07.30.00.00.00.html"]def parse(self, response):# pass# src、name、price都有共同的li标签# 所有的selector对象,都可以再次调用xpath方法li_list = response.xpath('//ul[@id = "component_59"]/li')for li in li_list:# .extract()提取数据# 有data-original,src用data-original替代src = li.xpath('.//img/@data-original').extract_first()# 第一张图片和其他图片标签不一样,第一张图片的src是可以使用的   其他图片的地址是data-originalif src:src = srcelse:# 用srcsrc = li.xpath('.//img/@src').extract_first()alt = li.xpath('.//img/@alt').extract_first()price = li.xpath('.//p[@class = "price"]/span[1]/text()').extract_first()print(src, name, price)book = DemoNddwItem(src = src, name = name, price = price)# yield:return一个返回值#获取一个book,就将book交给pipelinesyield book# pipelines(管道):想使用管道,必须在settings中开启管道:如何开启——》解除管道的注释即可开启
# ITEM_PIPELINES = {
#    "demo_nddw.pipelines.DemoNddwPipeline": 300,
# }

items.py

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass DemoNddwItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()# pass# 图片# src = // ul[ @ id = "component_59"] / li // img / @ srcsrc = scrapy.Field()# 名字# alt = // ul[ @ id = "component_59"] / li // img / @ altname = scrapy.Field()# 价格# price = //ul[@id = "component_59"]/li//p[@class = "price"]/span[1]/text()price = scrapy.Field()

4. scrapy使用步骤

1)在终端中创建项目

        scrapy startproject 项目名

2)切换到项目下的spiders目录

        cd 项目名\项目名\spider

3)在spiders中创建文件

        scrapy genspider 文件名  要爬取的网页

4)运行

        scrapy crawl 文件名


http://www.mrgr.cn/news/93393.html

相关文章:

  • 使用QT + 文件IO + 鼠标拖拽事件 + 线程 ,实现大文件的传输
  • 运动控制卡--固高实用
  • LLM-初识AI
  • 关于sqlalchemy的使用
  • 浏览器WEB播放RTSP
  • 深度学习PyTorch之13种模型精度评估公式及调用方法
  • 为AI聊天工具添加一个知识系统 之138 设计重审 之2 文章学 引言之2 附加符号学和附属诠释学
  • 前端基础入门-高级
  • WLAN无线组网 WI-FI
  • 如何改变怂怂懦弱的气质(2)
  • 从零构建企业级财务分析数仓 | Hive建模实战
  • Spring(四)先注册后注入
  • 为AI聊天工具添加一个知识系统 之138 设计重审 2 文章学 之2 结合诠释学的重组
  • Gazebo11 与 Protobuf 版本管理
  • Python:简单的爬虫程序,从web页面爬取图片与标题并保存MySQL
  • K8s 1.27.1 实战系列(五)Namespace
  • aws(学习笔记第三十一课) aws cdk深入学习(batch-arm64-instance-type)
  • MacOS Big Sur 11 新机安装brew wget python3.12 exo
  • ubuntu20.04已安装 11.6版本 cuda,现需要通过源码编译方式安装使用 cuda 加速的 ffmpeg 步骤
  • AI绘画软件Stable Diffusion详解教程(7):图生图基础篇