Scrapy程序未返回输出

fd3cxomn  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(185)

我 是 Python 的 新手 , 正在 尝试 使用 Scrapy 来 构建 一 个 Web Scraper 。 我 从 下面 的 URL 中 学习 了 教程 :https://doc.scrapy.org/en/latest/intro/tutorial.html 就 像 教程 中 一样 , 在 Visual Studio 代码 文本 编辑 器 中 运行 程序 后 , 我 检查 了 当前 的 文件 目录 , 但 找 不到 任何 HTML 文件 。
是 做 错 了 什么 还是 安装 错 了 ?
下面 是 代码

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

    def start_requests(self):
        urls =[
            'https://quotes.toscrape.com/page/1/'
            'https://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 = f'quotes-{page}.html'
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log(f'Saved file {filename}')

中 的 每 一 个

zfciruhq

zfciruhq1#

也许你自己输入了代码,而不是从教程中复制代码。你忽略了url后面的逗号,这导致了你的代码出现问题。

溶液:

将代码的以下部分放入

urls = [
            'https://quotes.toscrape.com/page/1/'
            'https://quotes.toscrape.com/page/2/'
        ]

替换为:

urls = [
            'https://quotes.toscrape.com/page/1/',
            'https://quotes.toscrape.com/page/2/'
        ]

注意:URL后面必须有逗号分隔。

相关问题