Selenium和Python,在使用xpath时获取动态src的子级

2024-04-28 16:20:19 发布

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

我有一些html我想刮

<div class="prw_rup prw_common_static_map_no_style staticMap" data-prwidget-name="common_static_map_no_style" data-prwidget-init="handlers">
    <div class="prv_map clickable" onclick="requireCallLast('ta/maps/opener', 'open', 2, null, null,{customFilters: []})">
         <img width="310" style="width:310px;height:270px;" id="lazyload_-1295083988_4" height="270" src="https://trip-raster.citymaps.io/staticmap?scale=2&amp;zoom=18&amp;size=310x270&amp;language=en&amp;center=32.769936,-117.252693&amp;markers=icon:http%3A%2F%2Fc1.tacdn.com%2Fimg2%2Fmaps%2Ficons%2Fpin_v2_CurrentCenter.png|32.769936,-117.25269&amp;markers=icon:http%3A%2F%2Fc1.tacdn.com%2Fimg2%2Fmaps%2Ficons%2Fpin_lg_Restaurant.png|32.769936,-117.25269|32.770027,-117.25272&amp;markers=icon:http%3A%2F%2Fc1.tacdn.com%2Fimg2%2Fmaps%2Ficons%2Fpin_lg_ThingToDo.png|32.77055,-117.25273|32.770683,-117.251884|32.770664,-117.25131">
    </div>
</div>

如何检索子div的src?意思是,我想以字符串的形式返回url

到目前为止,我最接近它的是

try:
    mappa = driver.find_element_by_xpath("""//*[@id="taplc_location_detail_overview_restaurant_0"]/div[1]/div[2]/div[1]/div""") # .get_attribute("src")
    print(mappa, "this is mappa")
    child_mappa = mappa.find_element_by_xpath('.//*').get_attribute("src")
    print(child_mappa)

这就产生了:

$ <selenium.webdriver.remote.webelement.WebElement (session="4c6acf0a93bc9c184a351ddbc2180977", element="0.5263477154236882-1")> 
$ https://static.tacdn.com/img2/x.gif

因为id是动态的,所以我不能用它来获取xpath。因为xpath与该ID相关。而且,为什么src会改变

一个人是怎么得到src的


Tags: divsrcidmappngstylestaticelement
1条回答
网友
1楼 · 发布于 2024-04-28 16:20:19

所以,它有点不稳定,但我设法用正则表达式得到它。我没有用selenium来获取它,而是读入所有的html,用regex查找url,并将其拆分到需要的地方

虽然不干净,但很管用

    driver.get(url)
    innerHTML = driver.execute_script("return document.body.innerHTML")
    print(type(innerHTML))

    try:
        urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', innerHTML)
        #print(urls)
        for page_url in urls:
            if 'staticmap?scale=' in page_url:
                map_click = page_url.split('language=en&center=')[1].split('&markers=icon:http')[0]
                lat, long = map_click.split(',')
                break
    except:
        lat, long = None, None

相关问题 更多 >