Skip to main content
 首页 » 编程设计

python之scrapy之每个 starurl 单独的输出文件

2024年10月01日6mayingbao

我有一个运行良好的 scrapy 蜘蛛:

`# -*- coding: utf-8 -*- 
import scrapy 
 
 
class AllCategoriesSpider(scrapy.Spider): 
    name = 'vieles' 
    allowed_domains = ['examplewiki.de'] 
    start_urls = ['http://www.exampleregelwiki.de/index.php/categoryA.html','http://www.exampleregelwiki.de/index.php/categoryB.html','http://www.exampleregelwiki.de/index.php/categoryC.html',] 
 
#"Titel": : 
 
def parse(self, response): 
    urls = response.css('a.ulSubMenu::attr(href)').extract() # links to den subpages 
    for url in urls: 
        url = response.urljoin(url) 
        yield scrapy.Request(url=url,callback=self.parse_details) 
 
def parse_details(self,response): 
    yield { 
        "Titel": response.css("li.active.last::text").extract(), 
        "Content": response.css('div.ce_text.first.last.block').extract(), 
    } 

` 与

scrapy runspider spider.py -o dat.json it saves all info to dat.json

我希望每个起始 url 都有一个输出文件 categoryA.json categoryB.json 等等。

A similar question一直没有回答,我无法重现this answer我无法从 suggestions there 中学习.

我如何实现拥有多个输出文件的目标,每个 starturl 一个? 我只想运行一个命令/shellscript/文件来实现这一点。

请您参考如下方法:

您没有在代码中使用真实的 url,所以我使用我的页面进行测试。
我必须更改 css 选择器并且我使用了不同的字段。

我将它保存为csv,因为它更容易追加数据。
JSON 需要从文件中读取所有项目,添加新项目并将所有项目再次保存在同一个文件中。


我创建了额外的字段 Category 以便稍后在管道中将其用作文件名

items.py

import scrapy 
 
class CategoryItem(scrapy.Item): 
    Title = scrapy.Field() 
    Date = scrapy.Field() 
    # extra field use later as filename  
    Category = scrapy.Field() 

在蜘蛛中,我从 url 获取类别并使用 Request 中的 meta 发送到 parse_details。
在 parse_details 中,我将 category 添加到 Item。

spiders/example.py

import scrapy 
 
class ExampleSpider(scrapy.Spider): 
    name = 'example' 
    allowed_domains = ['blog.furas.pl'] 
    start_urls = ['http://blog.furas.pl/category/python.html','http://blog.furas.pl/category/html.html','http://blog.furas.pl/category/linux.html'] 
 
    def parse(self, response): 
 
        # get category from url 
        category = response.url.split('/')[-1][:-5] 
 
        urls = response.css('article a::attr(href)').extract() # links to den subpages 
 
        for url in urls: 
            # skip some urls 
            if ('/tag/' not in url) and ('/category/' not in url): 
                url = response.urljoin(url) 
                # add category (as meta) to send it to callback function 
                yield scrapy.Request(url=url, callback=self.parse_details, meta={'category': category}) 
 
    def parse_details(self, response): 
 
        # get category 
        category = response.meta['category'] 
 
        # get only first title (or empty string '') and strip it 
        title = response.css('h1.entry-title a::text').extract_first('') 
        title = title.strip() 
 
        # get only first date (or empty string '') and strip it 
        date = response.css('.published::text').extract_first('') 
        date = date.strip() 
 
        yield { 
            'Title': title, 
            'Date': date, 
            'Category': category, 
        } 

在管道中,我得到 category 并使用它打开文件以附加和保存项目。

pipelines.py

import csv 
 
class CategoryPipeline(object): 
 
    def process_item(self, item, spider): 
 
        # get category and use it as filename 
        filename = item['Category'] + '.csv' 
 
        # open file for appending 
        with open(filename, 'a') as f: 
            writer = csv.writer(f) 
 
            # write only selected elements  
            row = [item['Title'], item['Date']] 
            writer.writerow(row) 
 
            #write all data in row 
            #warning: item is dictionary so item.values() don't have to return always values in the same order 
            #writer.writerow(item.values()) 
 
        return item 

在设置中,我必须取消注释管道才能激活它。

settings.py

ITEM_PIPELINES = { 
    'category.pipelines.CategoryPipeline': 300, 
} 

GitHub 上的完整代码:python-examples/scrapy/save-categories-in-separated-files


顺便说一句:我认为您可以直接在 parse_details 中写入文件。