scrapy在命令行上工作,但不是sublim

2024-05-19 15:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是sublime texteditor3,当我构建文件时,实际上什么都没做,当我从命令行运行时,它工作正常,我需要在sublime上安装软件包才能运行吗,或者我应该怎么做?(我在windows上使用python3.7)

这是来自scrapy网站的代码

import scrapy


class QuotesSpider(scrapy.Spider):
    name = "quotes"

    def start_requests(self):
        urls = [
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        page = response.url.split("/")[-2]
        filename = 'quotes-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Saved file %s' % filename)

我没有得到错误,但没有文件创建时,我在崇高运行。你知道吗


Tags: selfcomhttpurlparseresponsedefpage
1条回答
网友
1楼 · 发布于 2024-05-19 15:38:53

你不能直接从sublime跑蜘蛛。你知道吗

您必须创建包含内容的附加python文件

from scrapy import cmdline    
cmdline.execute("scrapy crawl spider_name".split())

用scrapy项目目录更改它的工作目录,然后运行它。你知道吗

相关问题 更多 >