今天,學習了scrapy框架pipelines處理數據部分。pipelines可以將由爬蟲文件傳遞來的數據保存到文件中。例如,將數據保存到.txt文件中。
首先,需要先設置pipelines.py文件。
# -*- coding: utf-8 -*-
import codecs
class MypjtPipeline(object):
def __init__(self):
self.file = codecs.open("D:/Kangbb/data1.txt", "w", encoding="utf-8")
def process_item(self, item, spider):
l = str(item['title'])+'\n'
self.file.write(l)
return item
def close_spider(self):
self.file.close()
process_item()函數必須定義,它是真正處理數據的函數。其他函數可選擇實現。
接着,設置setting.py。將下列部分的註釋去掉,根據自己定義的函數來填補內容:
#ITEM_PIPELINES = {
# 'mypjt.pipelines.MypjtPipeline': 300,
#}
ITEM_PIPELINES = {
'mypjt.pipelines.MypjtPipeline': 300,
}
其中,mypjt.pipelines.MypjtPipeline命名規範爲項目名.piplines配置文件名.piplines文件裏的類名。300代表優先級,範圍爲0~1000,數字越小,越先執行。可以同時定義多個處理數據的類,使用數字來確定優先級。
但是,當我完成這些以後,發現依舊不能保存數據到文件。經過多方面確定和查閱文檔,才發現原來是爬蟲文件(spiders文件夾下的文件)中的parse()函數沒有return語句所致。
初始版本:
# -*- coding: utf-8 -*-
import scrapy
from mypjt.items import MypjtItem
class MyfileSpider(scrapy.Spider):
name = 'myfile'
allowed_domains = ['www.jd.com']
start_urls = ['https://channel.jd.com/eleNews.html']
def parse(self, response):
item = MypjtItem()
item['title'] = response.xpath("/html/head/title/text()")
print(item['title'])
修改後版本:
# -*- coding: utf-8 -*-
import scrapy
from mypjt.items import MypjtItem
class MyfileSpider(scrapy.Spider):
name = 'myfile'
allowed_domains = ['www.jd.com']
start_urls = ['https://channel.jd.com/eleNews.html']
def parse(self, response):
item = MypjtItem()
item['title'] = response.xpath("/html/head/title/text()")
print(item['title'])
return item
這樣就解決了所有問題。
總結一下,要使用scrapy保存數據到文件,需要注意以下三點:
1. pipelines文件正確配置
2. 配置settings.py文件
3. 爬蟲文件parse()函數一定要由return語句