如何使用phantomjs将包含标记的地图保存到图像文件中

2024-05-20 13:42:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试保存一张包含标记和热图的地图
下面是显示地图的代码

import folium
m_f = folium.Map(location= [39.76258, 254.994682],
                 zoom_start=8)

m_f.save('f_map.html')
m_f

然后我使用phantomjs保存图像

import selenium.webdriver
import time

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='/Users/path/Downloads/PhantomJS/bin/phantomjs')

driver.set_window_size(4000, 3000)  # choose a resolution
driver.get('f_map.html')
# You may need to add time.sleep(seconds) here
time.sleep(1)
driver.save_screenshot('f_map.png')

在这里之前没有问题
然而,当我在地图上添加一些标记时,我得到了正确的html文件,我也可以在浏览器中打开它,但是它的屏幕截图是一个空白文件(内容)

这是新地图的代码

import folium
m_f = folium.Map(location= [39.74258, 254.993682],
                 zoom_start=12)

arr_test1 = [39.74258, 254.993682]
arr_test2 = [39.75258, 254.994682]
arr_test3 = [39.76258, 254.994682]

all_loc = [arr_test1, arr_test2, arr_test3]

for cur_loc in all_loc:
    point = folium.Circle(
        radius=200,
        location=cur_loc,
        popup='The Waterfront',
        color='red',
        fill=False,
    )
    point.add_to(m_f)
    
    time.sleep(0.001)
    
m_f
m_f.save('f_map_marker.html')

以及它的phantonjs代码行

import selenium.webdriver
import time

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='/Users/path/Downloads/PhantomJS/bin/phantomjs')

driver.set_window_size(4000, 3000)  # choose a resolution
driver.get('f_map_marker.html')

time.sleep(1)
driver.save_screenshot('f_map_marker.png')

因此,f_map_marker.png文件为空。
有人有想法吗?为什么会这样?我如何解决这个问题?
欢迎任何提示


Tags: pathimportmaptimesavehtmldriverselenium