脚本宝典收集整理的这篇文章主要介绍了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 目前是单线程 多线程还没学大佬们手下留情
以上是脚本宝典为你收集整理的Python爬虫 彼岸壁纸高清图片爬取全部内容,希望文章能够帮你解决Python爬虫 彼岸壁纸高清图片爬取所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。