scrapy 为什么我的JSON导出在Python中运行时不起作用?

brgchamk  于 2022-11-09  发布在  Python
关注(0)|答案(1)|浏览(98)

我已经创建了一个python scrapy脚本,我希望能够通过运行python文件而不使用终端来启动该脚本。
我一直在用下面的-

os.system("scrapy crawl preorder")

然而,当我添加一个JSON导出时,Scrapy会正常运行,但不会创建JSON文件。

os.system("scrapy crawl preorder -O test.json")

那么为什么JSON导出不起作用呢?
这是完整的代码-

import scrapy, os

from scrapy.crawler import CrawlerProcess

class PreOrderSpider(scrapy.Spider): 
    name = 'preorder'
    start_urls = ['https://www.kapowtoys.co.uk/category/preorders.html']

    def parse(self, response):
        for products in response.css('li.product'):
            try:
                yield{
                    'name': products.css('h2::text').get(),
                    'price': products.css('ins::text').get().replace('£',''),
                    'link': products.css('a').attrib['href'],
                }
            except:
                yield{
                    'name': products.css('h2::text').get(),
                    'price': 'unavailble', 
                    'link': products.css('a').attrib['href'],
                }

        next_page = response.css('a.next.page-numbers').attrib['href']
        if next_page is not None:
            yield response.follow(next_page, callback=self.parse)
            # if next page exists parse again

os.system("scrapy crawl preorder -O test.json")
2nbm6dog

2nbm6dog1#

谢谢,伙计们。因为终端位置默认为项目目录,我在错误的地方寻找文件。愚蠢,我。

相关问题