splash:set_viewport_full() 在 localhost 中工作正常,而当我运行 scrapy 代码时,它只保存了一半的图像

问题描述

我是 Scrapy-Splash 的新手,我想截取整个网页的屏幕截图。例如“https://stackoverflow.com/”。 在 lua 脚本中,我添加了以下代码-

function main(splash,args)
  assert(splash:go(args.url))
  assert(splash:wait(0.5))
  splash:set_viewport_full()
  return {
    html = splash:html(),png = splash:png(),har = splash:har(),}
end

我可以在 Splash loaclhost 中看到完整的网页截图。Here is the image. I have not downloaded it. I want to just show that it works fine here.

而我想直接将其保存在本地。我想通过scrapy将它们保存在本地,而不是从Splash localhost手动下载图像。所以我在 Scrapy-Splash-

中编写了以下代码
class TestSpider(scrapy.Spider):
    name = 'test'
    
    def start_requests(self):
        url='https://stackoverflow.com/'
        splash_args = {
            'html': 1,'png': 1,'wait':0.5
        }
        yield SplashRequest(url,self.parse_result,endpoint='render.json',args=splash_args)

    def parse_result(self,response):
        imgdata = base64.b64decode(response.data['png'])
        filename = 'stackoverflow.png'
        with open(filename,'wb') as f:
            f.write(imgdata)

下载此图像时,我看到图像被裁剪掉了。 Here is the screenshot of the cropped image after running the above code

当我运行此代码时,我获得了 200 成功,如下所示 - 这意味着代码是通过 splash localhost 运行的。

Crawled (200) <GET https://stackoverflow.com/ via http://localhost:8050/render.json> (referer: None)

我附上了两张图片。我再次成为 Scrapy-Splash 的新手。请帮我找到解决此问题的方法?谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)