如何获取实际图片URL?跳转后目标URL的获取及功能处理
Got it, let's cover different ways to capture that final redirected URL depending on your use case—whether you're working in the browser, a backend script, or just need to check manually:
If you need to fetch the final URL directly from a web page (to compare it against the browser's current URL), use the Fetch API with a HEAD request (it's more efficient than a full GET since we don't need the response body):
const initialUrl = 'http://cdn.amazon.com/picture/f184a6f7b9c4371cf962537ff384a362d97aa495044995079bca063aeb66d8c0'; fetch(initialUrl, { method: 'HEAD', redirect: 'follow' // Ensures we follow all redirects }) .then(response => { const finalRedirectedUrl = response.url; console.log('Final URL after redirects:', finalRedirectedUrl); // Compare with the browser's current URL if (finalRedirectedUrl === window.location.href) { // Run your custom functionality here console.log('URLs match—executing your logic...'); } }) .catch(error => { console.error('Failed to fetch redirect info:', error); });
If the page has already completed the redirect, you can simply grab the current URL directly with window.location.href—no extra fetch needed.
Using curl (quick command-line check)
Run this command to get the final effective URL without downloading any content:
curl -Ls -o /dev/null -w "%{url_effective}" "http://cdn.amazon.com/picture/f184a6f7b9c4371cf962537ff384a362d97aa495044995079bca063aeb66d8c0"
-L: Follows all redirects-s: Runs in silent mode (no progress output)-o /dev/null: Discards the response body-w "%{url_effective}": Prints the final URL after all redirects
Using Python (for backend automation)
Use the requests library (it follows redirects by default):
import requests initial_url = 'http://cdn.amazon.com/picture/f184a6f7b9c4371cf962537ff384a362d97aa495044995079bca063aeb66d8c0' # Use HEAD request for efficiency response = requests.head(initial_url, allow_redirects=True) final_url = response.url print(f"Final redirected URL: {final_url}") # Compare with your target URL target_url = 'http://cdn.amazon.com/picture/logo' if final_url == target_url: # Execute your processing logic here print("Redirect matches target URL—processing...")
If you just need to verify the redirect manually:
- Open your browser's DevTools (press F12 or Ctrl+Shift+I)
- Go to the Network tab
- Check the Preserve log checkbox (so redirect requests aren't cleared)
- Enter the initial URL in the address bar and hit enter
- Look for the first request (with a 3xx status code like 301/302)
- Click that request, then check the Response Headers section—you'll see a
Locationfield with the final redirected URL
内容的提问来源于stack exchange,提问作者user8842379




