Python爬虫 彼岸壁纸高清图片爬取
直接上代码
1 import re 2 from tool import mkdir 3 import requests 4 from bs4 import BeautifulSoup 5 6 7 def main(url): 8 msg = requests.get(url, headers=headers) 9 msg.encoding = "GBK" 10 bs = BeautifulSoup(msg.text, "lxml") 11 title = bs.select("head > title") 12 src = bs.select("#img > img") 13 srcpath = "https://pic.netbian.com/" + re.search(r'src="(.*?)" title', str(src[0])).groups()[0] 14 print(src[0]) 15 file = re.search(r'<title>(.*?)_', str(title[0])) 16 filename = file.groups()[0] # 文件名 17 print(filename) 18 dir = re.search(r'_(.*?)_', str(title[0])) 19 dirname = dir.groups()[0] # 目录名 20 print(dirname) 21 files = filepath + "\\" + dirname 22 mkdir(files) 23 files += "\\" + filename 24 msg = requests.get(srcpath) 25 openfile = open(files + ".jpg", "wb") 26 openfile.write(msg.content) 27 openfile.close() 28 29 if __name__ == '__main__': 30 headers = { 31 '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', 32 'accept-encoding': 'gzip, deflate, br', 33 'accept-language': 'zh-CN,zh;q=0.9', 34 'cache-control': 'max-age=0', 35 'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"', 36 'sec-ch-ua-mobile': '?0', 37 'sec-ch-ua-platform': '"Windows"', 38 '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' 39 } 40 41 filepath = input("请输入需要保存的目录路径:") 42 for i in range(1, 999999): 43 url = "https://pic.netbian.com/tupian/" + str(i) + ".html" 44 main(url)
以下为tool.py
import os def mkdir(path): path = path.strip()# 去除首位空格 path = path.rstrip("\\")# 去除尾部 \ 符号 isExists = os.path.exists(path) # 判断路径是否存在 # 判断结果 if not isExists: # 如果不存在则创建目录 os.makedirs(path) return True else: return False
效果图:
通过晚上嗷嗷内卷,复习了忘了的正则和xml 目前是单线程 多线程还没学大佬们手下留情
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?