如何解决如何使用pythonSeleniumWebscrap画布元素
我需要来自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 recieve 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()
解决方法
这是一个示例,单击画布并四处移动。也打印信息。
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
还有更多
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。