Python爬虫编程思想(152):使用Scrapy抓取数据,使用ItemLoader保存多条抓取的数据

360次阅读
没有评论
Python爬虫编程思想(152):使用Scrapy抓取数据,使用ItemLoader保存多条抓取的数据

        在上一篇文章中通过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:

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-25发表,共计573字。
新手QQ群:570568346,欢迎进群讨论 Python51学习