- 所需环境 python 3.7
- 所用到的模块 os,requests ,lxml
- 不说了上代码
# -*- coding: UTF-8 -*-import osimport requests
from lxml import etreeurl = "http://desk.zol.com.cn/qiche/1920x1080/"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
}def get_img(url):response = requests.get(url, headers=headers).contenthtml = etree.HTML(response)li_list = html.xpath("//li[@class='photo-list-padding']")cate_list = []for li in li_list:cate_url = "http://desk.zol.com.cn" + li.xpath("./a[@class='pic']/@href")[0]cate_list.append(cate_url)img_info = []for cate_url in cate_list:item = {}resp = requests.get(cate_url, headers=headers).contenthtml = etree.HTML(resp)item["img_url"] = html.xpath("//img[@id='bigImg']/@src")[0] # 获取图片的urlitem["img_name"] = html.xpath("//a[@id='titleName']/text()")[0]img_info.append(item)for item in img_info:resp = requests.get(item["img_url"], headers=headers).contentfile_name = item["img_name"]dir ="E:\\图片\\"if not os.path.exists(dir):os.makedirs(dir)print("在E盘已创建图片文件.......")print(file_name + "图片已下载")with open(dir + file_name + ".jpg", "wb") as f:f.write(resp)
def get_next_url(url):# 首页的图片print("开始下载图片")try:get_img(url)except Exception as e:print(e)get_img(url)while True:response = requests.get(url, headers=headers).contenthtml = etree.HTML(response)next_href = "http://desk.zol.com.cn" + html.xpath("//a[@id='pageNext']/@href")[0]url = next_hrefif not url:print("图片下载完毕")breakget_img(url)if __name__ == '__main__':get_next_url(url)
结果:
car.exe文件在百度网盘:链接:https://pan.baidu.com/s/19O5omfrkDJChWnqqj0awqQ
提取码:84v5