Background and Objective
To acquire a template from a foreign website, I attempted to use the wget tool to fetch web resources. Some image paths were embedded within a JavaScript file, for example:
patterns: [
'assets/images/layout/bg/arches.png',
'assets/images/layout/bg/blu_stripes.png',
// ... more paths
]
The goal is to extract the image filenames (e.g., arches.png) from this code and download them in batch.
Corrected Bash Script
The original script contained a syntax error. Here is the corrected, standard version:
#!/bin/bash
# Batch download PNG files
text="png.list"
url="http://www.example.com/assets/images/layout/bg"
download_files() {
while read -r line
do
filename=$(echo "$line" | tr -d '[:space:]')
if [ -n "$filename" ]; then
echo "Downloading: $filename"
wget -q "$url/$filename"
fi
done < "$text"
}
download_files
Script Explanation and Usage
- Prepare the file list: Save the image filenames (e.g.,
arches.png), one per line, in a text file namedpng.list. - Update the URL: Modify the
urlvariable in the script to the correct base path. - Run the script: In the terminal, grant execute permission and run it:
chmod +x download.sh ./download.sh
Extracting Filenames from a JS File
Use this command to quickly extract PNG filenames from a JavaScript snippet and create the png.list file:
grep -o "'[^']*.png'" customizer.js | sed "s/'//g" > png.list
This command finds all single-quoted strings ending with .png, removes the quotes, and writes the result to the file.
Important Notes
- Ensure
wgetis installed on your system. - If the target site requires authentication or has anti-scraping measures, you may need to add parameters like
--user-agentor--headerto thewgetcommand. - Respect the website's
robots.txtrules and terms of service to avoid placing excessive load on the server.