Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Downloading High-Resolution Wallpapers from Netbian with Python

Tech 1

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)

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.