利用scrapy爬取文件后并基于管道化的持久化存储
生活随笔
收集整理的這篇文章主要介紹了
利用scrapy爬取文件后并基于管道化的持久化存储
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們在pycharm上爬取
首先我們可以在本文件打開命令框或在Terminal下創建
scrapy startproject xiaohuaPro? ?------------創建文件
scrapy genspider xiaohua www.xxx.com? ----------創建執行文件
?
一.首先我們要進行數據的爬取
import scrapy from xioahuaPro.items import XioahuaproItemclass XiaohuaSpider(scrapy.Spider):name = 'xiaohua'start_urls=['http://www.521609.com/daxuemeinv/']#生成一個通用的url模板url = 'http://www.521609.com/daxuemeinv/list8%d.html'pageNum =1def parse(self, response):li_list=response.xpath('//div[@class="index_img list_center"]/ul/li')for li in li_list:name = li.xpath('./a[2]/text() | ./a[2]/b/text()').extract_first()img_url = 'http://www.521609.com'+li.xpath('./a[1]/img/@src').extract_first()#實例化一個item類型的對象item = XioahuaproItem()item['name'] = nameitem['img_url'] = img_url#item提交給管道yield item# 對其他頁碼的url進行手動i請求的發送if self.pageNum <= 24: ------爬取的頁數self.pageNum += 1new_url = format(self.url%self.pageNum)yield scrapy.Request(url=new_url,callback=self.parse)之后再items.py文件下為item對象設置屬性
將爬取到的所有信息全部設置為item的屬性
# -*- coding: utf-8 -*-# Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.htmlimport scrapyclass XioahuaproItem(scrapy.Item):# define the fields for your item here like:name = scrapy.Field()img_url = scrapy.Field()二.寫入pipelines.py內容
首先寫入到自定義的文件里去
作用:將解析到的數據存儲到某一個平臺中。 import pymysql from redis import Redis class XioahuaproPipeline(object):fp = Nonedef open_spider(self,spider):print('開始爬蟲!')self.fp = open('./xiaohua.txt','w',encoding='utf-8')#作用:實現持久化存儲的操作#該方法的item參數就可以接收爬蟲文件提交過來的item對象#該方法每接收一個item就會被調用一次(調用多次)def process_item(self, item, spider):name = item['name']img_url = item['img_url']self.fp.write(name+':'+img_url+'\n')#返回值的作用:就是將item傳遞給下一個即將被執行的管道類return item # def close_spider(self,spider):print('結束爬蟲!')self.fp.close() #寫到數據庫里面,我們要在數據庫里面創建個表(將mysql和redis都啟動)
class MysqlPipeline(object):conn = Nonecursor = Nonedef open_spider(self, spider):#解決數據庫字段無法存儲中文處理:alter table tableName convert to charset utf8;self.conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='123',db='test',charset='utf8')print(self.conn)def process_item(self, item, spider):self.cursor = self.conn.cursor()try:self.cursor.execute('insert into xiaohua values ("%s","%s")'%(item['name'],item['img_url']))self.conn.commit()except Exception as e:print(e)self.conn.rollback()return itemdef close_spider(self, spider):self.cursor.close()self.conn.close()在相同的文件下創建redis類寫入數據
class RedisPipeline(object):conn = Nonedef open_spider(self, spider):self.conn = Redis(host='127.0.0.1',port=6379)print(self.conn)def process_item(self, item, spider):dic = {'name':item['name'],'img_url':item['img_url']}print(str(dic))self.conn.lpush('xiaohua',str(dic))return itemdef close_spider(self, spider):pass三.更改配置文件,在settings.py里面
#添加上這行代碼USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'# Obey robots.txt rules ROBOTSTXT_OBEY = False -----改成False
ITEM_PIPELINES = {'xioahuaPro.pipelines.XioahuaproPipeline': 300, ---對應文件# 'xioahuaPro.pipelines.MysqlPipeline': 301, ----對應數據庫
# 'xioahuaPro.pipelines.RedisPipeline': 302, -----對應redis }LOG_LEVEL = 'ERROR'
# CRITICAL --嚴重錯誤
#ERROR ---一般錯誤
#WARNING ---警告信息
#INFO ---一般信息
#DEBUG --調試信息
然后我們在終端去指定爬蟲程序
scrapy? crawl? ? 名字(name對應的值)
轉載于:https://www.cnblogs.com/tianshuai1/p/10841290.html
總結
以上是生活随笔為你收集整理的利用scrapy爬取文件后并基于管道化的持久化存储的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷P2312解方程题解
- 下一篇: es6笔记 day3---Promise