我有一个必须创建屏幕截图的简单的抓蜘蛛。以下是我的代码,但出现错误:
回溯(最近通话最近): _runCallbacks中的第654行“ c:\ users \ xxxxx \ appdata \ local \ programs \ python \ python37 \ lib \ site-packages \ twisted \ internet \ defer.py” current.result =回调(current.result,* args,** kw) 文件“ C:\ Users \ xxxxxx \ scrapy_screenshot \ scrapy_screenshot \ spiders \ extract.py”,第19行,位于parse_result中 imgdata = base64.b64decode(response.data ['png']) AttributeError:“ HtmlResponse”对象没有属性“ data”
import json
import base64
import scrapy
from scrapy_splash import SplashRequest
class ExtractSpider(scrapy.Spider):
name = 'extract'
def start_requests(self):
url = 'https://stackoverflow.com/'
splash_args = {
'html': 1,
'png': 1
}
yield SplashRequest(url, self.parse_result, endpoint='render.json', args=splash_args)
def parse_result(self, response):
imgdata = base64.b64decode(response.data['png'])
filename = 'some_image.png'
with open(filename, 'wb') as f:
f.write(imgdata)