安装 :lxml
地址 : Installing lxml
pip install lxml 或者 sudo pip install lxml
下面开始 写代码 下载 站长素材的图片
import urllib.requestfrom lxml import etreeimport osdef create_request(page):if(page == 1):url = "https://sc.chinaz.com/tupian/qinglvtupian.html"else:url = "https://sc.chinaz.com/tupian/qinglvtupian_" + str(page) + '.html'# print(url,'ssss')headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 Edg/116.0.0.0"}request = urllib.request.Request(url=url, headers=headers)return requestdef get_content(request):response = urllib.request.urlopen(request)content = response.read().decode('utf-8')return contentdef down_load(content):tree = etree.HTML(content)srve_list = tree.xpath('//div[@class="item"]/img/@data-original')name_list = tree.xpath('//div[@class="item"]/img/@alt')# print(name_list , srve_list, 'src')for i in range(len(name_list)):name = name_list[i]src = srve_list[i]url = "https:" + srcprint(name,url, 'pinjie')## 指定保存图片的文件夹和文件名directory = './qinglvtupian'if not os.path.exists(directory):os.makedirs(directory) # 如果文件夹不存在,则创建它filename = os.path.join(directory, name + '.jpg')urllib.request.urlretrieve(url=url, filename=filename)# 不存文件夹# urllib.request.urlretrieve( url = url , filename= "./qinglvtupian" + name+ '.jpg')if __name__ == '__main__':start_page = int(input("请输入开始值"))end_page = int(input("请输入结束值"))for page in range(start_page, end_page + 1):# print(page)request = create_request(page)content = get_content(request)down_load(content)