婚纱摄影网站图片,关注公众号推广赚佣金,黄岩建设局网站,58同城遵义#x1f381;#x1f381;创作不易#xff0c;关注作者不迷路#x1f380;#x1f380; 目录
#x1f338;完整代码
#x1f338;分析 #x1f381;基本思路
#x1f381;需要的库
#x1f381;提取图片的链接和标题
#x1f453;寻找Cookie和User-Agent
创作不易关注作者不迷路 目录
完整代码
分析 基本思路
需要的库
提取图片的链接和标题
寻找Cookie和User-Agent
图片链接和标题
下载保存图片
获取目录页面图片和翻页提取
目录页图片的提取
翻页规律寻找
运行效果 文末彩蛋 我们经常想要寻找一些高清的壁纸图片作为素材为CSDN博客找一张吸引读者的封面然而一张一张的下载太慢了因此为了提高工作效率 我们可以采用爬虫的方式快速下载图片。 完整代码
import os#导入操作系统的库
import requests #导入HTTP库
from lxml import etree#导入lxml库数据解析global num
num1
#请求头,伪装爬虫
header{
user-agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0,
cookie:
zkhanecookieclassrecord%2C66%2C70%2C}#获取具体的图片的地址和名字信息
# urlhttps://pic.netbian.com/tupian/34694.html
def get_pic(url,header):rerequests.get(url,headersheader)re.encodingre.apparent_encoding#获取html文本时用网页原有的编码方式防止乱码#print(re.apparent_encoding) #返回的编码htmletree.HTML(re.text)linkhtml.xpath(//div[classphoto-pic]/a/img/src)[0]#获取图片链接linkhttps://pic.netbian.comlinkprint(link)titlehtml.xpath(//div[classphoto-pic]/a/img/title)[0]#获取图片名称print(title)return title,link#下载保存图片
def download_pic(url,header):global numtitle,linkget_pic(url,header)if not os.path.exists(rC:\Users\liu\Desktop\图片\4K壁纸):#未找到文件夹则创建文件夹os.mkdir(rC:\Users\liu\Desktop\图片\4K壁纸)contentrequests.get(link,headersheader).contentwith open(rfC:\Users\liu\Desktop\图片\4K壁纸\{str(num)}.jpg,wb) as f:#以二进制编码写入文件f.write(content)num 1#目录翻页提取链接
def get_content_link(url,header):# urlhttps://pic.netbian.com/pingban/index.htmlrerequests.get(url,headersheader)re.encodingre.apparent_encoding# print(re.text)htmletree.HTML(re.text)linkshtml.xpath(//div[classslist]//a/href)for x in links:xhttps://pic.netbian.comxdownload_pic(x,header)#循环遍历网页处理信息
for i in range(1,24):if i1:urlhttps://pic.netbian.com/pingban/index.htmlelse :urlfhttps://pic.netbian.com/pingban/index_{i}.htmlget_content_link(url,header)
分析 基本思路 找到图片页网页源代码提取所有图片的链接和标题下载保存图片爬取目录页的网页源代码下载目录页的图片分析不同页面的地址变化找出规律实现翻页下载
需要的库
import os
import requests
from lxml import etree
requests和lxml库是第三方库需要自己安装
提取图片的链接和标题
寻找Cookie和User-Agent
首先打开页面打开开发者工具按CtrlR刷新页面点击开发者工具的“网络”选项点击第一份文件查看请求地址Cookie和User-Agent 将Cookie和User-Agent作为请求头
header{
user-agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0,
cookie:
zkhanecookieclassrecord%2C66%2C70%2C}
图片链接和标题 这里需要用到lxml库以及xpath的知识看图说话链接和地址存在div classphoto-pic下的a元素中img元素中的src属性和title属性
图片链接
linkhtml.xpath(//div[classphoto-pic]/a/img/src)[0]#获取图片链接
图片标题
titlehtml.xpath(//div[classphoto-pic]/a/img/title)[0]#获取图片名称
写成函数方便调用
#获取具体的图片的地址和名字信息
# urlhttps://pic.netbian.com/tupian/34694.html
def get_pic(url,header):rerequests.get(url,headersheader)re.encodingre.apparent_encoding#获取html文本时用网页原有的编码方式防止乱码#print(re.apparent_encoding) #返回的编码htmletree.HTML(re.text)linkhtml.xpath(//div[classphoto-pic]/a/img/src)[0]#获取图片链接linkhttps://pic.netbian.comlinkprint(link)titlehtml.xpath(//div[classphoto-pic]/a/img/title)[0]#获取图片名称print(title)return title,link
下载保存图片
存储到一个新的文件夹“4K壁纸”如果文件夹不存在需要创建这里要用到os库
#未找到文件夹则创建文件夹
if not os.path.exists(rC:\Users\liu\Desktop\图片\4K壁纸): os.mkdir(rC:\Users\liu\Desktop\图片\4K壁纸)
写入文件
contentrequests.get(link,headersheader).content
with open(rfC:\Users\liu\Desktop\图片\4K壁纸\{str(num)}.jpg,wb) as f:#以二进制编码写入文件f.write(content)
写成函数方便调用
#下载保存图片
def download_pic(url,header):global numtitle,linkget_pic(url,header)if not os.path.exists(rC:\Users\liu\Desktop\图片\4K壁纸):#未找到文件夹则创建文件夹os.mkdir(rC:\Users\liu\Desktop\图片\4K壁纸)contentrequests.get(link,headersheader).contentwith open(rfC:\Users\liu\Desktop\图片\4K壁纸\{str(num)}.jpg,wb) as f:#以二进制编码写入文件f.write(content)num 1
获取目录页面图片和翻页提取
上面我们实现一张图片的保存写了十几行代码算是成功保存了一张图片干嘛这么麻烦捏直接点击“图片另存为”不就行了吗那如果是很多图片吗那肯定是爬虫更快了呗
目录页图片的提取
依然用到lxml库利用xpath语法提取 #目录翻页提取链接
def get_content_link(url,header):# urlhttps://pic.netbian.com/pingban/index.htmlrerequests.get(url,headersheader)re.encodingre.apparent_encoding# print(re.text)htmletree.HTML(re.text)linkshtml.xpath(//div[classslist]//a/href)for x in links:xhttps://pic.netbian.comxdownload_pic(x,header)
翻页规律寻找
找到第一页目录页
https://pic.netbian.com/pingban/index.html
找到第二页目录页
https://pic.netbian.com/pingban/index_2.html
找到第三页目录页
https://pic.netbian.com/pingban/index_3.html
发现规律:第一页单独列出来其他页通过for循环改变index_{i}即可
#循环遍历网页处理信息
for i in range(1,24):if i1:urlhttps://pic.netbian.com/pingban/index.htmlelse :urlfhttps://pic.netbian.com/pingban/index_{i}.htmlget_content_link(url,header)
通过for循环遍历最终可以实现所有图片的下载
运行效果
成功下载4K壁纸耗时两分半下载400多张图片爬虫提取就是快手动提取预估一坤时左右 文末彩蛋