繁体   English   中英

如何从 div 样式 selenium/python 中提取背景图像 URL?

[英]How to extract background-image URL from div style selenium/python?

<div style="left: 0%;background-image: url(&quot;https://i.xxxx.com/pictures/3724063802.jpg?type=original&quot;);width: 100%;height: 100%;background-size: contain;background-position: 50% 50%;background-repeat: no-repeat;position: absolute;"></div>

如何使用 Python 从 selenium 中的上述 html 中定位和提取背景图像 URL? 我的问题是我之前用过id,没有id怎么办?

我建议使用 XPath 来查找 div( find_element_by_xpath("//div") ),您可以对这些进行索引以找到您要查找的特定 div 元素( //div[3] , //div[0]等)

要提取背景图像,请使用value_of_css_property()方法。

bg_url = div.value_of_css_property('background-image') # 'url("https://i.xxxx.com/img.jpg")'
# strip the string to leave just the URL part
bg_url = bg_url.lstrip('url("').rstrip('")')
# https://i.xxxx.com/img.jpg

另一种方法是执行find_elements_by_tag_name('div')因为这将返回一个 div 列表,但您必须筛选它们以找到您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM