Yes. You can do that by using scrapy-splash plugin.
https://github.com/scrapy-plugins/scrapy-splash
Please read the "More complex Splash Lua Script example - get a screenshot of an HTML element by its CSS selector (it requires Splash 2.1+). Note how are arguments passed to the script:" part there.
You do not use render.png endpoint, you use execute endpoint and execute a lua script to get a screenshot of a specific element.
I implemented it like this in scrapy spider.
def start_requests(self):
print('parse_start_url')
yield SplashRequest(url, self.parse_result,
endpoint='execute',
args={
'lua_source': script,
'pad': 32,
'css': 'body'
}
)
script = """
-- Arguments:
-- * url - URL to render;
-- * css - CSS selector to render;
-- * pad - screenshot padding size.
-- this function adds padding around region
function pad(r, pad)
return {r[1]-pad, r[2]-pad, r[3]+pad, r[4]+pad}
end
-- main script
function main(splash)
-- this function returns element bounding box
local get_bbox = splash:jsfunc([[
function(css) {
var el = document.querySelector(css);
var r = el.getBoundingClientRect();
return [r.left, r.top, r.right, r.bottom];
}
]])
assert(splash:go(splash.args.url))
assert(splash:wait(0.5))
-- don't crop image by a viewport
splash:set_viewport_full()
local region = pad(get_bbox(splash.args.css), splash.args.pad)
return splash:png{region=region}
end
"""