Downloading High-Resolution Wallpapers from Netbian with Python
Define a helper function to ensure directory existence:
import os
def ensure_directory_exists(directory_path):
if not os.path.exists(directory_path):
os.makedirs(directory_path)
Implement the main proecssing functino:
import os
import requests
from bs4 import BeautifulSoup
def retrieve_wallpaper_page(page_url, storage_root):
request_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Cache-Control': 'max-age=0',
'Sec-Ch-Ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"'
}
response = requests.get(page_url, headers=request_headers)
response.encoding = 'GBK'
parsed_html = BeautifulSoup(response.text, 'lxml')
page_title = parsed_html.title.string
title_segments = page_title.split('_')
image_prefix = title_segments[0] if title_segments else 'image'
category_name = title_segments[1] if len(title_segments) > 1 else 'default'
picture_element = parsed_html.select_one('#img > img')
if picture_element is None:
return
img_location = picture_element.get('src')
full_img_url = img_location if img_location.startswith('http') else 'https://pic.netbian.com' + img_location
target_directory = os.path.join(storage_root, category_name)
ensure_directory_exists(target_directory)
img_data = requests.get(full_img_url).content
img_filename = f"{image_prefix}.jpg"
complete_path = os.path.join(target_directory, img_filename)
with open(complete_path, 'wb') as output_file:
output_file.write(img_data)
if __name__ == '__main__':
root_storage = input("Enter the directory path for saving images: ")
for page_num in range(1, 999999):
target_url = f"https://pic.netbian.com/tupian/{page_num}.html"
retrieve_wallpaper_page(target_url, root_storage)