什么是框架?php
如何学习框架:html
掌握框架的功能,能够熟练使用每一种功能便可.python
scrapy:mysql
环境的安装:
a. pip3 install wheelredis
b. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted c. 进入下载目录,执行 pip3 install Twisted‑17.1.0‑cp35‑cp35m‑win_amd64.whl d. pip3 install pywin32 e. pip3 install scrapy 测试:在终端中录入scrapy
使用流程:sql
#UA假装 USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' #不听从爬虫Robots协议 ROBOTSTXT_OBEY = False #只输出错误信息日志 LOG_LEVEL = 'ERROR'
scrapy的数据解析数据库
scrapy的持久化存储app
#基于终端指令的持久化存储代码示例,直接写在你的项目文件中便可, #运行指令scrapy crawl qiubai -o filePath # -*- coding: utf-8 -*- import scrapy # from qiubaiPor.items import QiubaiporItem class QiubaiSpider(scrapy.Spider): name = 'qiubai' # allowed_domains = ['www.xxx.com'] # 存放在该列表中的url都会被scrapy自动的进行请求发送 start_urls = ['https://www.qiushibaike.com/text/'] #基于终端指令的持久化存储:能够将parse方法的返回值对应的数据进行本地磁盘文件的持久化存储 def parse(self, response): all_data = [] # 数据解析:做者and段子内容 div_list = response.xpath('//div[@id="content-left"]/div') for div in div_list: # 在scrapy中使用xpath解析标签中的文本内容的话,最终获取的是一个Selector的对象,且咱们须要的字符串数据所有被封装在了该对象中 #若是能够肯定xpath返回的列表只有一个列表元素则使用extract_first(),不然使用extract() author = div.xpath('./div[1]/a[2]/h2/text()').extract_first() content = div.xpath('./a/div/span/text()').extract() dic = {"author":author,"content":content} all_data.append(dic) return all_data
#基于sarapy模块进行的数据爬取存储 # -*- coding: utf-8 -*- import scrapy from qiubaiPro.items import QiubaiproItem class QiubaiSpider(scrapy.Spider): name = 'qiubai' # allowed_domains = ['www.xxx.com'] #存放在该列表中的url都会被scrapy自动的进行请求发送 start_urls = ['https://www.qiushibaike.com/text/'] #基于终端指令的持久化存储:能够将parse方法的返回值对应的数据进行本地磁盘文件的持久化存储 # def parse(self, response): # all_data = [] # #数据解析:做者and段子内容 # div_list = response.xpath('//div[@id="content-left"]/div') # for div in div_list: # #在scrapy中使用xpath解析标签中的文本内容的话,最终获取的是一个Selector的对象,且咱们须要的字符串数据所有被封装在了该对象中 # #若是能够肯定xpath返回的列表只有一个列表元素则使用extract_first(),不然使用extract() # author = div.xpath('./div[1]/a[2]/h2/text()').extract_first() # content = div.xpath('./a/div/span/text()').extract() # # dic = { # 'author':author, # 'content':content # } # all_data.append(dic) # # print(author,content) # return all_data #基于管道实现持久化存储 def parse(self, response): all_data = [] #数据解析:做者and段子内容 div_list = response.xpath('//div[@id="content-left"]/div') for div in div_list: #在scrapy中使用xpath解析标签中的文本内容的话,最终获取的是一个Selector的对象,且咱们须要的字符串数据所有被封装在了该对象中 #若是能够肯定xpath返回的列表只有一个列表元素则使用extract_first(),不然使用extract() author = div.xpath('./div[1]/a[2]/h2/text()').extract_first() if not author: author = '匿名用户' content = div.xpath('./a/div/span/text()').extract() content = ''.join(content) #建立一个item类型的对象(只能够存储一组解析的数据) item = QiubaiproItem() #将解析到的数据存储到item对象中 item['author'] = author item['content'] = content #将item提交给管道类 yield item
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import pymysql from redis import Redis #一个管道类对应一种平台的数据存储 class QiubaiproPipeline(object): fp = None #重写父类的方法:只在开始爬虫的时候被执行一次 def open_spider(self,spider): print('开始爬虫......') self.fp = open('./qiubai.txt','w',encoding='utf-8') #处理item类型的对象 #什么是处理? #将封装在item对象中的数据值提取出来且进行持久化存储 #参数item表示的就是爬虫文件提交过来的item对象 #该方法每接收一个item就会被调用一次 def process_item(self, item, spider): print('this is process_item()') author = item['author'] content = item['content'] self.fp.write(author+':'+content+"\n") #返回的item就会传递给下一个即将被执行的管道类 return item def close_spider(self,spider): print('结束爬虫!') self.fp.close() #将数据同时存储到mysql class mysqlPileLine(object): conn = None cursor = None def open_spider(self,spider): self.conn = pymysql.Connect(host='127.0.0.1',port=3306,db='spider',user='root',password='',charset='utf8') print(self.conn) def process_item(self,item,spider): sql = 'insert into qiubai values ("%s","%s")'%(item['author'],item['content']) #建立一个游标对象 self.cursor = self.conn.cursor() try: self.cursor.execute(sql) self.conn.commit() except Exception as e: print(e) self.conn.rollback() return item def close_spider(self,spider): self.cursor.close() self.conn.close() class redisPileLine(object): conn = None def open_spider(self,spider): self.conn = Redis(host='127.0.0.1',port=6379) def process_item(self,item,spider): dic = { 'author':item['author'], 'content':item['content'] } self.conn.lpush('qiubaiData',dic)
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class QiubaiporItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() author = scrapy.Field() content = scrapy.Field()
ITEM_PIPELINES = { 'qiubaiPro.pipelines.QiubaiproPipeline': 300, # 'qiubaiPro.pipelines.mysqlPileLine': 301, 'qiubaiPro.pipelines.redisPileLine': 302, #300表示的是优先级,数值越小优先级越高 }
进行全站数据的爬取
- 手动请求的发送(get请求)
- yield scrapy.Request(url,callback)
- callback:用于数据解析框架
def start_requests(self): for url in self.start_urls: data = {} yield scrapy.FormRequest(url,callback=self.parse,formdata=data)
# -*- coding: utf-8 -*- import scrapy #爬取多页 from qiubaiByPages.items import QiubaibypagesItem class QiubaiSpider(scrapy.Spider): name = 'qiubai' # allowed_domains = ['www.xxx.com'] start_urls = ['https://www.qiushibaike.com/text/'] #定制一个通用的url模板 url = 'https://www.qiushibaike.com/text/page/%d/' pageNum = 1 def parse(self, response): print('正在爬取{}页......'.format(self.pageNum)) all_data = [] # 数据解析:做者and段子内容 div_list = response.xpath('//div[@id="content-left"]/div') for div in div_list: # 在scrapy中使用xpath解析标签中的文本内容的话,最终获取的是一个Selector的对象,且咱们须要的字符串数据所有被封装在了该对象中 # 若是能够肯定xpath返回的列表只有一个列表元素则使用extract_first(),不然使用extract() author = div.xpath('./div[1]/a[2]/h2/text()').extract_first() if not author: author = '匿名用户' content = div.xpath('./a/div/span/text()').extract() content = ''.join(content) # 建立一个item类型的对象(只能够存储一组解析的数据) item = QiubaibypagesItem() # 将解析到的数据存储到item对象中 item['author'] = author item['content'] = content # 将item提交给管道类 yield item #递归解析+手动请求发送 ==> 全站数据爬取 if self.pageNum <= 13: self.pageNum += 1 new_url = format(self.url%self.pageNum) yield scrapy.Request(url=new_url,callback=self.parse)
class QiubaibypagesPipeline(object): fp = None # 重写父类的方法:只在开始爬虫的时候被执行一次 def open_spider(self, spider): print('开始爬虫......') self.fp = open('./qiubai.txt', 'w', encoding='utf-8') # 处理item类型的对象 # 什么是处理? # 将封装在item对象中的数据值提取出来且进行持久化存储 # 参数item表示的就是爬虫文件提交过来的item对象 # 该方法每接收一个item就会被调用一次 def process_item(self, item, spider): # print('this is process_item()') author = item['author'] content = item['content'] self.fp.write(author + ':' + content + "\n") # 返回的item就会传递给下一个即将被执行的管道类 return item def close_spider(self, spider): print('结束爬虫!') self.fp.close()
最后不要忘记settings跟items文件的配置dom
爬取http://www.521609.com/meinvxiaohua/ 图片数据
#运行文件内容 import scrapy from xiaohuaPro.items import XiaohuaproItem class XiaohuaSpider(scrapy.Spider): name = 'xiaohua' # allowed_domains = ['www.xxx.com'] start_urls = ['http://www.521609.com/meinvxiaohua/'] url = 'http://www.521609.com/meinvxiaohua/list12%d.html' page_num = 1 def parse(self, response): li_list = response.xpath('//*[@id="content"]/div[2]/div[2]/ul/li') for li in li_list: #建立一个对象 item = XiaohuaproItem() img_src = 'http://www.521609.com'+li.xpath('./a[1]/img/@src').extract_first() title = li.xpath('./a[1]/img/@alt').extract_first() item['title'] = title item['img_src'] = img_src yield item if self.page_num <21: self.page_num += 1 new_url = format(self.url%self.page_num) #使用yield方式进行反复的递归回调,来获取新的内容 yield scrapy.Request(new_url,self.parse)
items中定义字段
import scrapy class XiaohuaproItem(scrapy.Item): # define the fields for your item here like: title = scrapy.Field() img_src = scrapy.Field() pass
pipelines中接收并进行存储
from scrapy.pipelines.images import ImagesPipeline import scrapy class XiaohuaproPipeline(object): def process_item(self, item, spider): print(item) return item #使用scrapy专门封装好的一个管道类(ImagesPipeline)文件数据下载和持久化存储 class imgPileLine(ImagesPipeline): #进行文件请求 def get_media_requests(self, item, info): yield scrapy.Request(item['img_src']) #指定文件最终持久化存储对应的文件名称 def file_path(self, request, response=None, info=None): img_src = request.url img_name = img_src.split('/')[-1] return img_name def item_completed(self, results, item, info): print(results) return item #能够将item传递给下一个即将被执行的管道类
在这些代码操做以前不要忘了对你的工程项目进行settings的配置
#UA假装 USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' #不听从爬虫Robots协议 ROBOTSTXT_OBEY = False #只显示报错信息 LOG_LEVEL = 'ERROR' #管道优先级 TEM_PIPELINES = { 'xiaohuaPro.pipelines.XiaohuaproPipeline': 301, 'xiaohuaPro.pipelines.imgPileLine': 300, } #设置文件夹存储路径 IMAGES_STORE = './xiaohuas'
scrapy的五大核心组件
引擎(Scrapy)
调度器(Scheduler)
下载器(Downloader)
爬虫(Spiders)
项目管道(Pipeline)
请求传参:
基于scrapy框架的对电影及详情页内容的爬取案例
import scrapy from moviePro.items import MovieproItem class MovieSpider(scrapy.Spider): name = 'movie' # allowed_domains = ['www.xxx.com'] start_urls = ['https://www.4567tv.tv/index.php/vod/show/id/5.html'] #封装一个通用的url模板 url = 'https://www.4567tv.tv/index.php/vod/show/id/5/page/%d.html' page_num = 1 def parse(self, response): print('正在爬取第{}页......'.format(self.page_num)) li_list = response.xpath('/html/body/div[1]/div/div/div/div[2]/ul/li') for li in li_list: title = li.xpath('./div/a/@title').extract_first() detail_url = 'https://www.4567tv.tv'+li.xpath('./div/a/@href').extract_first() item = MovieproItem() item['title'] = title #对详情页发起get请求 #meta是一個字典,将meta传递给回调函数 yield scrapy.Request(detail_url,callback=self.parse_detail,meta={'item':item}) if self.page_num <= 33: self.page_num += 1 new_url = format(self.url%self.page_num) yield scrapy.Request(new_url,callback=self.parse) #用来解析详情页中的电影简介 def parse_detail(self,response): #提取meta item = response.meta['item'] desc = response.xpath('/html/body/div[1]/div/div/div/div[2]/p[5]/span[2]/text()').extract_first() item['desc'] = desc yield item
import scrapy class MovieproItem(scrapy.Item): # define the fields for your item here like: #电影名称 title = scrapy.Field() #电影详情介绍 desc = scrapy.Field()
class MovieproPipeline(object): def process_item(self, item, spider): print(item) return item