【问题标题】:How to webscrape a canvas element with Python (Selenium)如何使用 Python (Selenium) 抓取画布元素
【发布时间】:2020-10-22 03:06:00
【问题描述】:

我需要来自https://childcaredeserts.org/2018/index.html?state=ID 的信息,特别是当我将鼠标悬停在某个状态时弹出的信息。我尝试过使用 selenium 和 python(我是网络抓取的新手),但我不知道如何获取地图的信息,地图似乎是一个画布元素(在 HTML5 中使用过调查) ,所以我对如何做到这一点有点困惑。

欢迎任何帮助。如有必要,我愿意使用其他编程语言。 也许我不太了解我正在努力解决的画布元素。

到目前为止我的代码:

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600')
driver = webdriver.Chrome('../Downloads/chromedriver', options=options)

driver.get('https://childcaredeserts.org/2018/index.html?state=WA');
action = ActionChains(driver); 
time.sleep(20)

element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "f1sovyv3")))

#here is where I define the path of info, not sure if it's ok
test = driver.find_elements_by_xpath("//*[name()='div' and @id='root']/*[name()='div']") 

res = []
for el in test:
    hover = action.move_to_element(el) 
    hover.perform() 
    #Below here is supposed to receive the info and append it to a list called res
    #licensed_child_care_providers
    #family_child_care_homes
    #state
    #res.append((licensed_child_care_providers, family_child_care_homes, state)))

for i in res:
    print(i)

driver.quit()

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping html5-canvas


    【解决方案1】:

    这是一个点击画布并四处移动的示例。也打印信息。

    canvas = driver.find_element_by_css_selector("canvas.mapboxgl-canvas")
    action.move_to_element(canvas).click().perform()
    box=driver.find_element_by_xpath('//*[@id="root"]/div/div[2]')
    print(box.text)
    action.move_by_offset(-50, -50).click().perform()
    box=driver.find_element_by_xpath('//*[@id="root"]/div/div[2]')
    print(box.text)
    

    输出

    Census Tract 9612
    Chelan County
    Child Care Desert
    Licensed child care providers: 0
    Family child care homes: 0
    Total child care capacity: 0
    Total population: 4682
    Population under age 5: 219
    Median family income: $60,788
    Percent of children with all parents in the labor force: 80%
    Maternal labor force participation: 76%
    Percent non-Hispanic, white: 69%
    Percent non-Hispanic, black/African American: 0%
    Percent Hispanic/Latino: 27%
    Children per licensed child care slot: No licensed child care providers
    

    还有更多。

    【讨论】:

    • 在这种情况下,您指向的是一个特定的像素,不是吗?我想要一个列表或迭代一个州的所有区域以获取每个“Cencus Tract”的信息,有什么方法吗?
    猜你喜欢
    • 1970-01-01
    • 2020-01-24
    • 2019-05-09
    • 2022-01-19
    • 2021-07-22
    • 1970-01-01
    • 2020-06-21
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多