import os import requests from bs4 import BeautifulSoup import time import random # 设置请求头,模拟浏览器访问 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'Referer': 'https://pic.netbian.com/' } def download_image(url, save_path): """下载图片并保存到指定路径""" try: response = requests.get(url, headers=headers, stream=True) if response.status_code == 200: with open(save_path, 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print(f"图片下载成功: {save_path}") else: print(f"下载失败,状态码: {response.status_code}") except Exception as e: print(f"下载图片时出错: {e}") def get_netbian_images(page_url, max_images=20): """爬取彼岸图网指定页面的图片""" # 创建保存图片的文件夹 save_dir = "netbian_4k_images" os.makedirs(save_dir, exist_ok=True) try: # 获取页面内容 response = requests.get(page_url, headers=headers) if response.status_code != 200: print(f"请求失败,状态码: {response.status_code}") return soup = BeautifulSoup(response.text, 'html.parser') # 查找图片列表 - 彼岸图网的结构 ul_element = soup.find('ul', {'class': 'clearfix'}) if not ul_element: print("未找到图片列表") return li_elements = ul_element.find_all('li') downloaded = 0 for li in li_elements: if downloaded >= max_images: break a_tag = li.find('a') if not a_tag: continue # 获取详情页链接 detail_url = "https://pic.netbian.com" + a_tag['href'] # 访问详情页获取高清大图 try: detail_resp = requests.get(detail_url, headers=headers) if detail_resp.status_code != 200: continue detail_soup = BeautifulSoup(detail_resp.text, 'html.parser') img_div = detail_soup.find('div', {'class': 'photo-pic'}) if not img_div: continue img_tag = img_div.find('img') if not img_tag: continue # 获取图片URL img_url = "https://pic.netbian.com" + img_tag['src'] # 提取文件名 file_name = img_url.split('/')[-1] save_path = os.path.join(save_dir, file_name) # 下载图片 if not os.path.exists(save_path): # 避免重复下载 download_image(img_url, save_path) downloaded += 1 # 随机延迟,避免请求过于频繁 time.sleep(random.uniform(1, 3)) except Exception as e: print(f"处理详情页时出错: {e}") continue except Exception as e: print(f"爬取过程中出错: {e}") if __name__ == "__main__": page_url = "https://pic.netbian.com/4k/index_61.html" max_images = int(input("请输入最大下载数量(默认为20): ") or 20) get_netbian_images(page_url, max_images) print("图片下载完成!") 生成讲解视频

视频信息