R/Python:从 Google 地图中提取信息

sta*_*oob 5 html python xml json r

我正在使用 R 和 Python 语言。

假设我在 Google 地图上搜索以下加拿大邮政编码 (M5V 3L9):

https://www.google.com/maps/place/多伦多,+ON+M5V+3L9/@43.642566,-79.3875851,18z/data=!4m6!3m5!1s0x882b34d436f9c825:0x9e9c6195e38030f2!8m2!3d43.6429129!4 d- 79.3853443!16s%2Fg%2F1tvq4rqd?条目=ttu

当我搜索此内容时,我可以看到该邮政编码的“周长”以红色突出显示:

在此输入图像描述

我的问题:( 通过 R/Python 使用 Selenium)从 HTML/CSS/XML 角度来看 - 我试图获取构成该周界边界的所有坐标的列表。

我一直在尝试探索从该网站生成的源代码,看看是否可以做一些事情来查看该周边的源代码(例如 JSON 格式)的存储位置 - 但到目前为止,我可以没有找到任何东西:

在此输入图像描述

我希望也许有一些东西可以让我使用 Selenium 反复单击此周边并提取经度/纬度点 - 但到目前为止,我找不到任何东西。

有人可以告诉我该怎么做吗?

谢谢!

注意:通用 Selenium 代码:

library(RSelenium)
library(wdman)
library(netstat)

selenium()
seleium_object <- selenium(retcommand = T, check = F)

remote_driver <- rsDriver(browser = "chrome", chromever = "114.0.5735.90", verbose = F, port = free_port())

remDr<- remote_driver$client


remDr$navigate("https://www.google.com/maps/place/Toronto,+ON+M5V+3L9/@43.642566,-79.3875851,18z/data=!4m6!3m5!1s0x882b34d436f9c825:0x9e9c6195e38030f2!8m2!3d43.6429129!4d-79.3853443!16s%2Fg%2F1tvq4rqd?entry=ttu")   
Run Code Online (Sandbox Code Playgroud)

Hum*_*man 1

您可以使用 PyAutoGui 库在屏幕上查找红色轮廓的十六进制值,然后将鼠标移动到该点,右键单击,然后使用另一个库的文本识别(如 pytesseract)来扫描该点的纬度和经度坐标出现在右键菜单中。我不确定文本识别,但是 PyAutoGui 部分很容易实现,大约 10 行代码。这是如何实现这一点的示例:

import pyautogui

def find_and_right_click(color):
    screen_width, screen_height = pyautogui.size()
    for x in range(screen_width):
        for y in range(screen_height):
            pixel_color = pyautogui.pixel(x, y)
            if pixel_color == color:
                pyautogui.moveTo(x, y)
                pyautogui.rightClick()

target_color = (234, 68, 54)

find_and_right_click(target_color)
Run Code Online (Sandbox Code Playgroud)

以下是如何使用 Selenium 完成部分工作:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
actions = ActionChains(driver)
actions.context_click(element).perform() #right click
Run Code Online (Sandbox Code Playgroud)

根据我的理解,你无法用selenium获取单个像素的rgb值,但你可以获取元素的rgb值。这意味着我们将无法找到要将光标移动到的像素(红色边界)的 rgb 值。安装 pyautogui 会容易得多(pip install pyautogui