在上一篇文章中通过ItemLoader保存了一条抓取的数据,如果要保存多条或所有抓取的数据,就需要parse方法返回一个MyscrapyItem数组。
下面的例子仍然会抓取上一篇文章例子中的博客列表页面,但会保存抓取页面所有的博客数据,包括每条博客的标题、摘要和Url。
import scrapy
from scrapy.loader import *
from scrapy.loader.processors import *
from bs4 import *
from myscrapy.items import MyscrapyItem
class ItemLoaderSpider1(scrapy.Spider):
name = 'ItemLoaderSpider1'
start_urls = [
'https://geekori.com/blogsCenter.php?uid=geekori'
]
def parse(self,response):
# 要返回的MyscrapyItem对象数组
items = []
# 获取博客页面的博客列表数据
sectionList = response.xpath('//*[@id="all"]/div[1]/section').extract()
# 通过循环迭代处理每一条博客列表数据
for section in sectionList:
