我正在尝试从https://www.billboard.com/charts/hot-100/2019-12-28抓取图像URL。
这是示例(span类='chart-element__image flex--no-shrink')
<span class="chart-element__image flex--no-shrink" style="background-image: url("https://charts-static.billboard.com/img/1994/12/mariah-carey-99g-all-i-want-for-christmas-is-you-3w0-155x155.jpg");"></span>
如您所见,范围具有属性“样式”,该属性显示与我的抓取输出不同的值。 见下文
from selenium import webdriver
def get_selenium_image_list(url):
""""given a top chart URL returns image list with selenium """
driver = webdriver.Chrome() # need to install chrome with specific driver
driver.get(url)
driver.fullscreen_window()
selenium_query = driver.find_element_by_xpath('//span[@class="chart-element__image flex--no-shrink"]')
image_list = []
for element in selenium_query:
image_list.append(element.get_attribute('style'))
return image_list
get_selenium_image_list('https://www.billboard.com/charts/hot-100/2019-12-28')
结果:
display: inline-block; height: 0px; width: 0px;
display: inline-block; height: 0px; width: 0px;
display: inline-block; height: 0px; width: 0px;
display: inline-block; height: 0px; width: 0px;
display: inline-block; height: 0px; width: 0px;
display: inline-block; height: 0px; width: 0px;
....
有人可以帮助我了解为什么会这样吗?以及如何解决这个问题?谢谢!