无法使用BeautifulSoup访问Div内的img

问题描述 投票:0回答:4
python html selenium-webdriver xpath beautifulsoup
4个回答
0
投票

您的 xpath 看起来有错误

//div[@class^=artistAndEventInfo-7c13900b']/img'

应该是

//div[@class='artistAndEventInfo-7c13900b']/img'

0
投票

如果你想获取图像的src,那么你应该使用下面的代码和更正后的xpath。

print(driver.find_element_xpath("//div[@class='artistAndEventInfo-7c13900b']//img").get_attribute("src"))

如果您想使用选项 1 和 2,请确保您获得如下属性

src

print image['src']

0
投票

使用 BeautifulSoup,你可以这样做:

from bs4 import BeautifulSoup

html = ''' <div class="artistAndEventInfo-7c13900b">
   <a class="artistAndEventInfo-48455a81" href="https://www.bandsintown.com/a/11985-perkele?came_from=257&amp;utm_medium=web&amp;utm_source=artist_event_page&amp;utm_campaign=artist">
       <img src="https://assets.bandsintown.com/images/fallbackImage.png" alt="">
       </a>
'''

soup = BeautifulSoup(html,'html5lib')

img = soup.find('img')

src = img['src']

print(src)

0
投票

您的 div 标签类属性值可能是动态的。您可以尝试下面的方法,而不是使用完整的类属性值。

from bs4 import BeautifulSoup
html='''<div class="artistAndEventInfo-7c13900b">
   <a class="artistAndEventInfo-48455a81" href="https://www.bandsintown.com/a/11985-perkele?came_from=257&amp;utm_medium=web&amp;utm_source=artist_event_page&amp;utm_campaign=artist">
       <img src="https://assets.bandsintown.com/images/fallbackImage.png" alt="">
       </a>'''
soup=BeautifulSoup(html,'lxml')
image = soup.select_one('div[class^=artistAndEventInfo-] img')
print(image['src'])
© www.soinside.com 2019 - 2024. All rights reserved.