从SVG交互式地图获取链接

阿萨夫·普拉斯(Asaf Pras)

我正在使用webdriver.Chrome(硒),通过以下网址获取存储在svg交互式地图下的链接:https : //www.mpcb.gov.in/water-quality

我曾尝试使用像xpath,partial_text等不同的find_elements_by_功能,但没有成功。这是我的代码的开始:

DRIVER_PATH = 'C:/Users/Asaf/Desktop/Asaf/Python/chromedriver.exe'
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
 
# Site URL
url='https://www.mpcb.gov.in/water-quality'
driver.get(url)

为了说明这一点,我想获取这些链接(地图的可单击区域),以便一次从它们中抓取数据。

DebanjanB

要从svg交互式地图获取链接,请在新标签页中打开链接以从其中抓取数据,一次可以使用以下“定位器策略”

  • 代码块:

    driver.get('https://www.mpcb.gov.in/water-quality')
    hrefs = [my_elem.get_attribute("xlink:href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[property='schema:text'] svg g a")))]
    parent_window  = driver.current_window_handle
    for href in hrefs:
        driver.execute_script("window.open('" + href +"')")
        WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
        windows_after = driver.window_handles
        new_window = [x for x in windows_after if x != parent_window][0]
        driver.switch_to_window(new_window)
        print(driver.current_url)
        driver.close()
        driver.switch_to_window(parent_window)
    driver.quit()
    
  • 控制台输出:

    https://www.mpcb.gov.in/water-quality/Thane/15
    https://www.mpcb.gov.in/water-quality/Nashik/18
    https://www.mpcb.gov.in/water-quality/Aurangabad/19
    https://www.mpcb.gov.in/water-quality/Amravati/21
    https://www.mpcb.gov.in/water-quality/Raigad/14
    https://www.mpcb.gov.in/water-quality/Pune/17
    https://www.mpcb.gov.in/water-quality/Nagpur/20
    https://www.mpcb.gov.in/water-quality/Chandrapur/23
    https://www.mpcb.gov.in/water-quality/Kolhapur/22
    https://www.mpcb.gov.in/water-quality/Mumbai/12
    

参考

您可以在以下位置找到几个相关的详细讨论:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章