You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python+Selenium获取内联CSS中的背景图片URL

Extract Background Image URL from Inline CSS with Selenium (Python)

Got it, let's walk through how to pull that background-image URL from your element using Selenium in Python. The key here is grabbing the inline style attribute and then parsing out the URL inside the url() part.

Step 1: Set Up Selenium and Locate the Element

First, make sure you have Selenium installed and your browser driver set up. Then, find the target element—since your example has an id="pic", we can use that to locate it easily.

Step 2: Grab the Inline Style Attribute

Once you've got the element, retrieve its style attribute. This will give you the full string of inline CSS rules.

Step 3: Parse the URL with Regex

Using a regular expression is the most robust way to extract the URL from the background-image property. This handles cases where the URL might be wrapped in quotes, or have extra characters like the stray semicolon in your example.

Full Code Example

from selenium import webdriver
from selenium.webdriver.common.by import By
import re

# Initialize the browser driver (Chrome in this case)
driver = webdriver.Chrome()

# Navigate to your target page
driver.get("https://your-target-page.com")

try:
    # Locate the element using its ID
    target_element = driver.find_element(By.ID, "pic")
    
    # Get the entire inline style attribute
    style_content = target_element.get_attribute("style")
    
    # Regex pattern to match background-image: url(...)
    # Captures everything inside the url() parentheses
    url_pattern = r'background-image:\s*url\(([^)]+)\)'
    match_result = re.search(url_pattern, style_content)
    
    if match_result:
        # Extract the raw URL string
        raw_image_url = match_result.group(1).strip()
        
        # Clean up the URL: remove quotes, extra semicolons, etc.
        cleaned_url = raw_image_url.replace('"', '').replace("'", '').replace(';', '')
        
        print(f"Extracted Background Image URL: {cleaned_url}")
    else:
        print("Could not find a background-image property in the element's style.")
finally:
    # Make sure to close the driver
    driver.quit()

Key Notes

  • Locators: If the element doesn't have an ID, use other locators like By.CLASS_NAME, By.CSS_SELECTOR, or By.XPATH to find it.
  • Regex Robustness: The regex pattern r'background-image:\s*url\(([^)]+)\)' accounts for whitespace between background-image: and url(, and captures everything inside the parentheses (even if there are extra characters like semicolons).
  • Cleanup: The cleaned_url step removes any quotes (single or double) and stray semicolons that might be present in the URL string (like in your example where the URL ends with ;).

This should work for your specific example and most other cases where the background image is set via inline CSS.

内容的提问来源于stack exchange,提问作者Anjali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:49:22