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

使用Python的BeautifulSoup4提取DIV标签style属性中的URL

Solution to Extract the Background Image URL

Extracting that specific URL from the style attribute is straightforward, and there are a few ways to do it depending on your use case. Here are the most common methods:

1. Using Browser Developer Tools (Quickest for One-Time Extraction)

If you just need to grab the URL quickly without writing code, follow these steps:

  • Right-click the panoramic element on the page and select "Inspect" to open DevTools.
  • In the Elements tab, find the #myPano div (or its child #myPanoCycle—both have the same URL).
  • Look at the style attribute, find background-image or background, and copy the URL inside the url(...) wrapper (make sure to remove any surrounding quotes or " entities).

Alternatively, run this one-liner in the DevTools Console to get it automatically:

document.getElementById('myPano').style.backgroundImage.replace(/^url\(["']?/, '').replace(/["']?\)$/, '')

This will strip the url("/url(' prefix and ")/') suffix, leaving you with the clean URL.

2. Using JavaScript in Your Code

If you need to extract this URL programmatically in your web app, use this approach:

// Target the parent div with id "myPano"
const panoElement = document.getElementById('myPano');

// Extract the background image URL from its style
function getBackgroundUrl(element) {
  const bgImage = element.style.backgroundImage;
  // Remove the url() wrapper and any quotes
  return bgImage.replace(/^url\(["']?/, '').replace(/["']?\)$/, '');
}

const targetUrl = getBackgroundUrl(panoElement);
console.log(targetUrl); // Outputs your desired URL

This function works for both single quotes, double quotes, or no quotes around the URL in the style attribute.

3. Using Regex for Text Processing (If Working with Raw HTML)

If you're handling the HTML as a raw string (e.g., in a script or text editor), use a regular expression to match the URL inside the url(...) pattern:

const html = '<div id="myPano" class="pano" style="background-image: url(&quot;https://www.stax.shop/pub/media/catalog/product/4/5/45c1383853439896cdaf594c318aab7d96c8fb502f22b3be0dc6e35c9e976ee4_7.jpg&quot;); background-position: 3676px center; background-size: auto 100%; background-repeat: repeat-x;"> <div id="myPanoCycle" class="cycle" style="background:url(https://www.stax.shop/pub/media/catalog/product/4/5/45c1383853439896cdaf594c318aab7d96c8fb502f22b3be0dc6e35c9e976ee4_7.jpg);height:900px;cursor: grab;visibility:hidden;"></div> </div>';

// Regex to capture URLs inside url()
const urlRegex = /url\(["']?([^"')]+)["']?\)/g;
let match;
const urls = [];

// Find all matching URLs
while ((match = urlRegex.exec(html)) !== null) {
  // Decode HTML entities like &quot; if present
  const decodedUrl = match[1].replace(/&quot;/g, '"');
  urls.push(decodedUrl);
}

console.log(urls); // Will include your target URL twice (once from each div)

This regex handles both quoted and unquoted URLs, and decodes any HTML entities like &quot; that might be in the raw HTML.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 12:23:15