如何在地图上截取js生成的活动数据

2024-10-04 03:20:41 发布

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

我是python的新用户,我想从以下网站获取数据:https://www.telerad.be/Html5Viewer/index.html?viewer=telerad_fr

我的问题是数据是动态生成的。我读到很少有可能修复,但没有一个是令人满意的。对于selenium,我需要一个名称或Xpath来单击按钮,但这里什么都没有。你知道吗

import requests
from lxml import html

page = requests.get('https://www.telerad.be/Html5Viewer/index.html?viewer=telerad_fr')
tree = html.fromstring(page.content)

cities = tree.xpath('//*[@id="map-container"]/div[6]/div[2]/div/div[2]/div/div/div[1]/div/p[1]/text()[2]')


print('Cities: ', cities)

Tags: httpsimportdivtreeindexhtmlwwwpage
1条回答
网友
1楼 · 发布于 2024-10-04 03:20:41

实际上有一个xpath可以点击按钮:

//*[@id='0_layer']/*[@fill]

来,试试这个(硒):

dotList = driver.find_elements_by_xpath("//*[@id='0_layer']/*[@fill]")
for dot in dotList:
    dot.click()
    cities = driver.find_element_by_xpath("//div[@data-region-name='NavigationMapRegion']//p[1]")
    print("Cities: ", cities.text)
    closeBtn = driver.find_element_by_xpath("//*[@class='panel-header-button right close-16']")
    closeBtn.click(); #the modal can intercept clicks on some dots, thats why we close it here after extracting the info we need.

这段代码单击(或者至少尝试,如果没有StaleElementExceptions发生)地图上的所有橙色点,并打印“Cities”内容(基于Xpath)。你知道吗

如果有人在代码中发现错误,请编辑这个答案,我写在记事本++。你知道吗

相关问题 更多 >