Web 从交互式网络地图中抓取屏幕图像

Web Scraping the screen image from an interactive web map

我需要从以下位置将地图组件提取到静态图像: http://www.bom.gov.au/water/landscape/#/sm/Relative/day/-35.30/145.17/5/Point////2018/12/16/

此页面包含一个基于 Leaflet 的交互式网络地图,其中图层数据通过网络地图服务每天更新。提取的图像应包含地图上加载的任何图层。

这也需要自动化,这样就没有人会打开网络浏览器并转到 URL。提取的图像将转到 Word 文档。

我是一名 Python 和 nodejs 程序员,但我无法通过 BeautifulSoup for Python 或 Cheerio for nodejs for web scraping 实现它,因为地图不是 img 元素但一些动态 DIV。如何将地图组件作为图像?

您可以使用:

from PIL import Image
from selenium import webdriver

driver = webdriver.Firefox()
driver.maximize_window() # maximize window
driver.get("http://www.bom.gov.au/water/landscape/#/sm/Relative/day/-35.30/145.17/5/Point////2018/12/16/")
element = driver.find_element_by_xpath("//*[@id=\"mapid\"]"); # this is the map xpath
location = element.location;
size = element.size;
driver.save_screenshot("canvas.png");
x = location['x'];
y = location['y'];
width = location['x']+size['width'];
height = location['y']+size['height'];
im = Image.open('canvas.png')
im = im.crop((int(x), int(y), int(width), int(height)))
im.save('canvas_el.png') # your file

如果需要遍历每一层,使用如下代码:

from time import sleep
driver.find_elements_by_class_name("leaflet-control-layers-toggle")[0].click(); # make layer selector visible
layers = driver.find_elements_by_class_name("leaflet-control-layers-selector"); # select each layer and wait 5seconds
for layer in layers:
    layer.click()
    sleep(5)
    # you can also capture screenshots here