日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python分布式爬虫及数据存储_二十一 Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫数据保存...

發布時間:2023/12/4 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python分布式爬虫及数据存储_二十一 Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫数据保存... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注意:數據保存的操作都是在pipelines.py文件里操作的

將數據保存為json文件

spider是一個信號檢測

# -*- coding: utf-8 -*-

# Define your item pipelines here

#

# Don't forget to add your pipeline to the ITEM_PIPELINES setting

# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.pipelines.images import ImagesPipeline #導入圖片下載器模塊

import codecs

import json

class AdcPipeline(object): #定義數據處理類,必須繼承object

def __init__(self):

self.file = codecs.open('shuju.json', 'w', encoding='utf-8') #初始化時打開json文件

def process_item(self, item, spider): #process_item(item)為數據處理函數,接收一個item,item里就是爬蟲最后yield item 來的數據對象

# print('文章標題是:' + item['title'][0])

# print('文章縮略圖url是:' + item['img'][0])

# print('文章縮略圖保存路徑是:' + item['img_tplj']) #接收圖片下載器填充的,圖片下載后的路徑

#將數據保存為json文件

lines = json.dumps(dict(item), ensure_ascii=False) + '\n' #將數據對象轉換成json格式

self.file.write(lines) #將json格式數據寫入文件

return item

def spider_closed(self,spider): #創建一個方法繼承spider,spider是一個信號,當前數據操作完成后觸發這個方法

self.file.close() #關閉打開文件

class imgPipeline(ImagesPipeline): #自定義一個圖片下載內,繼承crapy內置的ImagesPipeline圖片下載器類

def item_completed(self, results, item, info): #使用ImagesPipeline類里的item_completed()方法獲取到圖片下載后的保存路徑

for ok, value in results:

img_lj = value['path'] #接收圖片保存路徑

# print(ok)

item['img_tplj'] = img_lj #將圖片保存路徑填充到items.py里的字段里

return item #將item給items.py 文件的容器函數

#注意:自定義圖片下載器設置好后,需要在

將數據保存到數據庫

我們使用一個ORM框架sqlalchemy模塊,保存數據

數據庫操作文件

#!/usr/bin/env python

# -*- coding:utf-8 -*-

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import Column

from sqlalchemy import Integer, String, TIMESTAMP

from sqlalchemy import ForeignKey, UniqueConstraint, Index

from sqlalchemy.orm import sessionmaker, relationship

from sqlalchemy import create_engine

#配置數據庫引擎信息

ENGINE = create_engine("mysql+pymysql://root:279819@127.0.0.1:3306/cshi?charset=utf8", max_overflow=10, echo=True)

Base = declarative_base() #創建一個SQLORM基類

class SendMsg(Base): #設計表

__tablename__ = 'sendmsg'

id = Column(Integer, primary_key=True, autoincrement=True)

title = Column(String(300))

img_tplj = Column(String(300))

def init_db():

Base.metadata.create_all(ENGINE) #向數據庫創建指定表

def drop_db():

Base.metadata.drop_all(ENGINE) #向數據庫刪除指定表

def session():

cls = sessionmaker(bind=ENGINE) #創建sessionmaker類,操作表

return cls()

# drop_db() #刪除表

# init_db() #創建表

pipelines.py文件

# -*- coding: utf-8 -*-

# Define your item pipelines here

#

# Don't forget to add your pipeline to the ITEM_PIPELINES setting

# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.pipelines.images import ImagesPipeline #導入圖片下載器模塊

from adc import shujuku as ORM #導入數據庫文件

class AdcPipeline(object): #定義數據處理類,必須繼承object

def __init__(self):

ORM.init_db() #創建數據庫表

def process_item(self, item, spider): #process_item(item)為數據處理函數,接收一個item,item里就是爬蟲最后yield item 來的數據對象

print('文章標題是:' + item['title'][0])

print('文章縮略圖url是:' + item['img'][0])

print('文章縮略圖保存路徑是:' + item['img_tplj']) #接收圖片下載器填充的,圖片下載后的路徑

mysq = ORM.session()

shuju = ORM.SendMsg(title=item['title'][0], img_tplj=item['img_tplj'])

mysq.add(shuju)

mysq.commit()

return item

class imgPipeline(ImagesPipeline): #自定義一個圖片下載內,繼承crapy內置的ImagesPipeline圖片下載器類

def item_completed(self, results, item, info): #使用ImagesPipeline類里的item_completed()方法獲取到圖片下載后的保存路徑

for ok, value in results:

img_lj = value['path'] #接收圖片保存路徑

# print(ok)

item['img_tplj'] = img_lj #將圖片保存路徑填充到items.py里的字段里

return item #將item給items.py 文件的容器函數

#注意:自定義圖片下載器設置好后,需要在

總結

以上是生活随笔為你收集整理的python分布式爬虫及数据存储_二十一 Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫数据保存...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。