尚硅谷爬虫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 文件名