4K美女图片地址 http://pic.netbian.com/4kmeinv/
import requests
from lxml import etree
import os# 反爬三件套
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36','Referer':'http://pic.netbian.com/4kmeinv/index_3.html','Cookie': '__cfduid=d90d958cb3367809bc17573a227281fc31606608442; Hm_lvt_526caf4e20c21f06a4e9209712d6a20e=1606609463,1606698611,1606698612; zkhanecookieclassrecord=%2C66%2C54%2C; Hm_lpvt_526caf4e20c21f06a4e9209712d6a20e=1606723385'
}
# 图片下载
def download(pic_img, pic_name):r = requests.get(pic_img, headers=headers)pic = r.contenttry:with open('{}.jpg'.format(pic_name), 'wb') as f:f.write(pic)print('{}.jpg-----下载成功'.format(pic_name))except:print('下载失败!')def get_img(img_url):r = requests.get(img_url, headers=headers)html = r.content.decode('gbk')tree = etree.HTML(html)# 图片链接img_list = tree.xpath('//*[@id="main"]/div[3]/ul/li/a/img/@src')# 名字链接name_list = tree.xpath('//*[@id="main"]/div[3]/ul/li/a/b/text()')count = 1for pic_name, img in zip(name_list, img_list):img = 'http://pic.netbian.com' + img# 下载图片download(img, pic_name)count = count + 1# 定义主函数获取图片下载链接
def main():# 启始urlurl = 'http://pic.netbian.com/4kmeinv/'req = requests.get(url, headers=headers)html = req.content.decode('gbk')# print(html)tree = etree.HTML(html)# 提取壁纸页数num = tree.xpath('//*[@id="main"]/div[4]/a[7]/text()')[0]for i in range(int(num)):# 创建目录路径用于下载save_path = r'D:\python\4k美女\pic'.format(i + 1)# 每次写入文件之前首先清空if not os.path.exists(save_path):os.makedirs(save_path)# 改变当前工作目录os.chdir(save_path)print('第{}页:正在下载'.format(i + 1))img_url = url + 'index_{}.html'.format(i + 1)if i == 0:img_url = url# print(img_url)# 获取图片链接get_img(img_url)if __name__ == '__main__':main()