Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 刮擦飞溅屏幕截图管道不工作_Python_Scrapy_Scrapy Splash - Fatal编程技术网

Python 刮擦飞溅屏幕截图管道不工作

Python 刮擦飞溅屏幕截图管道不工作,python,scrapy,scrapy-splash,Python,Scrapy,Scrapy Splash,我正试图用Scrapy Splash保存被刮过的网页截图。我已将此处找到的代码复制并粘贴到我的管道文件夹中: 以下是url中的代码: import scrapy import hashlib from urllib.parse import quote class ScreenshotPipeline(object): """Pipeline that uses Splash to render screenshot of every Scrapy item."""

我正试图用Scrapy Splash保存被刮过的网页截图。我已将此处找到的代码复制并粘贴到我的管道文件夹中:

以下是url中的代码:

import scrapy
import hashlib
from urllib.parse import quote


class ScreenshotPipeline(object):
    """Pipeline that uses Splash to render screenshot of
    every Scrapy item."""

    SPLASH_URL = "http://localhost:8050/render.png?url={}"

    async def process_item(self, item, spider):
        encoded_item_url = quote(item["url"])
        screenshot_url = self.SPLASH_URL.format(encoded_item_url)
        request = scrapy.Request(screenshot_url)
        response = await spider.crawler.engine.download(request, spider)

        if response.status != 200:
            # Error happened, return item.
            return item

        # Save screenshot to file, filename will be hash of url.
        url = item["url"]
        url_hash = hashlib.md5(url.encode("utf8")).hexdigest()
        filename = "{}.png".format(url_hash)
        with open(filename, "wb") as f:
            f.write(response.body)

        # Store filename in item.
        item["screenshot_filename"] = filename
        return item
我还按照此处提供的设置splash的说明进行了操作:

当我调用命令
scrapy crawl spider
时,除了管道之外,其他一切都正常工作。 这就是我看到的“错误”

<coroutine object ScreenshotPipeline.process_item at 0x7f29a9c7c8c0>

爬行器正在正确生成项目,但不会处理该项目

有人有什么建议吗?多谢各位

编辑:

我认为现在的情况是,Scrapy正在像通常一样调用process_item()方法。然而,根据这些文档:必须以不同的方式调用协同程序对象

asyncio.run(process\u item())而不是process\u item()。
我想我可能需要修改源代码?

您应该在spider内部而不是管道中的脚本中使用scrapy splash


我遵循了这一点,它对我很有用。

Scrapy中的协同程序支持是最近才出现的,而且非常有限,请确保您阅读了您是否使用了docker容器?