使用 python selenium 抓取谷歌地球 [英] Google Earth scraping using python selenium

查看:50
本文介绍了使用 python selenium 抓取谷歌地球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 earth.google.com/web 创建一个网络抓取工具.每当用户在按住 shift 按钮的同时单击时,脚本将打印显示在谷歌地球网页右下角的坐标.

I want to create a web scraper for earth.google.com/web. Whenever the user clicks while holding shift button, the script will print the coordinates which are displayed at the bottom right corner of the google earth web page.

我在 chromedriver 中使用 selenium,但它找不到坐标 Web 元素.我尝试过 css 选择器、xpath、完整的 x 路径、按 id 查找.没有任何效果.

I am using selenium with chromedriver but it cannot find the coordinates web element. I have tried css selector, xpath, full x-path, find by id. Nothing worked.

这是我的代码:

import mouse
import keyboard
import time
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options)

driver.get('https://earth.google.com/web')

while True:
    if mouse.is_pressed(button='left') and keyboard.is_pressed('shift'):
        coordinates = driver.find_elements_by_id('pointer-coordinates')
        if len(coordinates) > 0:
            print(coordinates[0].text)
        else:
            print('No coordinates found!')
        time.sleep(0.2)

推荐答案

元素在 shadow 根元素内,需要使用查询选择器来识别元素.Induce javascript executor.

The element is inside shadow root element you need to use query selector to identify the element.Induce javascript executor.

import time

driver.get("https://earth.google.com/web")
time.sleep(10)
corordinate=driver.execute_script("return document.querySelector('earth-app').shadowRoot.querySelector('earth-view-status').shadowRoot.querySelector('span#pointer-coordinates')")
print(corordinate.text)
print(corordinate.get_attribute("textContent"))

这篇关于使用 python selenium 抓取谷歌地球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆