如何利用Python和Selenium从地图(例如Pokevision)中提取GIS坐标?

2024-04-25 11:35:47 发布

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

我想刮掉口袋妖怪,这样我可以得到所有的经纬度坐标显示口袋妖怪。在

网页的URL包含旗标的经度和纬度。例如,以下url包含39.95142302373031,-75.17986178398132:https://pokevision.com/#/@39.95142302373031,-75.17986178398132

source code中,标志标记具有以下div:

<div class="leaflet-marker-pane"><img class="leaflet-marker-icon leaflet-zoom-animated leaflet-clickable" src="/asset/image/leaflet//marker-icon.png" style="margin-left: -12px; margin-top: -41px; width: 25px; height: 41px; transform: translate(324px, 135px); z-index: 135;" tabindex="0"/>

我还注意到,每一个口袋妖怪都有一个div,如下所示:

^{pr2}$

我假设pokemon和flag标记的位置可以在div中找到,尤其是在文本“transform:translate(”)之后。在

考虑到我们既知道了象素位置,也知道了旗子的经纬度,还有口袋妖怪的像素位置,我相信我应该能够得到口袋妖怪的经纬度。在

例如,旗标的坐标总是324px,135px,我们知道旗标的gis坐标是39.95142302373031,-75.17986178398132。我们也知道口袋妖怪的坐标(例如215px,113px)。然而,我似乎不知道如何获得口袋妖怪的经度和纬度。在


Tags: 标记margindiv网页transformmarkertranslateclass
1条回答
网友
1楼 · 发布于 2024-04-25 11:35:47

如果单击地图,则URL将使用该点的坐标更新。你可以在地图上找到所有可见的口袋妖怪,点击它们,然后从更新的网址解析坐标。示例代码:

from pprint import pprint as pp

from selenium import webdriver
from selenium.common.exceptions import WebDriverException

poke_names = {
    21: "Spearow",
    23: "Ekans",
    39: "Jigglypuff",
    98: "Krabby",
    129: "Pidgey",

}

driver = webdriver.Chrome()
try:
    driver.get("https://pokevision.com/#/@39.95142302373031,-75.17986178398132")

    # Zoom out once
    zoom_css = "a.leaflet-control-zoom-out"
    driver.find_element_by_css_selector(zoom_css).click()

    # Find all pokemon in the source
    poke_css = "div.leaflet-marker-pane div.leaflet-marker-icon-wrapper"
    pokemon = driver.find_elements_by_css_selector(poke_css)
    print("Found {0} pokemon".format(len(pokemon)))

    # Filter for only the ones that are displayed on screen
    on_screen_pokemon = [p for p in pokemon if p.is_displayed()]
    print("There are {0} pokemon on screen".format(len(on_screen_pokemon)))

    # Click each pokemon, which moves the marker and thus updates the URL with
    # the coords of that pokemon
    coords = list()
    for pokemon in on_screen_pokemon:
        try:
            pokemon.click()
            # Example URL: https://ugc.pokevision.com/images/pokemon/21.png
            img_url = pokemon.find_element_by_css_selector('img').get_attribute("src")
            img_num = int(img_url.split('.png')[0].split('/')[-1])
        except WebDriverException:
            # Some are hidden by other elements, move on
            continue
        else:
            # Example
            # https://pokevision.com/#/@39.95142302373031,-75.17986178398132
            poke_coords = driver.current_url.split('#/@')[1].split(',')
            poke_name = poke_names[img_num] if img_num in poke_names else "Unknown"
            coords.append((poke_name, poke_coords))

    print("Found coordinates for {0} pokemon".format(len(coords)))
    for poke_name, poke_coords in coords:
        print("Found {0} pokemon at coordinates {1}".format(poke_name, poke_coords))

finally:
    driver.quit()

输出:

^{pr2}$

这段代码之所以有问题有几个原因,其中最主要的原因是过于宽泛和粗心的异常处理。但是,您应该能够将这个概念应用到更健壮的解决方案中。在

相关问题 更多 >