挂梯子后出现访问https报错信息报 hostname xxxx的错误,requests不到链接,解决方法
需求:读Excel中第一列作为文件夹名,第二列开始都是图片链接,图片链接可以有多条,每一行可以图片链接数不一,把每一行的图片下载到指定文件夹中。
test.xlsx例如:
import pandas as pd import requests import numpy as np import re import os import time # 读取Excel中Sheet1中的数据 data = pd.Dataframe(pd.read_excel('test.xlsx', 'Sheet1')) # 查看读取数据内容 print(data) print(data.values[0:, 0])#取第一列文件夹名 i=0 row =0 for dir_name in data.values[0:,0]: #遍历每一行 dir_name_real = re.sub(r'[/:*"<>|?n]*', '', str(dir_name)) #Re清洗一下,确保建立文件夹成功 os.mkdir(dir_name_real) for column in range(1,data.shape[1]): #从第二列开始遍历每一条图片链接 if pd.isna(data.iloc[row].iat[column])==False: #判断当前遍历的excel格子是否空了 website =data.iloc[row].iat[column] print(website) print("*"*50) #proxy = {"http":"127.0.0.1:7890"}; headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36' } try: requests.packages.urllib3.disable_warnings() response = requests.get(website, headers=headers,verify = False,timeout=10) print(response.status_code) if response.status_code == 200: response.encoding = response.apparent_encoding patten = "https://.*/(.+).jpg|https://.*/(.+).png" #修改就是改这里 # if website[-3:] == 'jpg': with open("./" + dir_name_real + "/" + str(re.search(patten, website).group(1)) + ".jpg", "wb") as f: print("正在保存第 %d 个网站的图片" % (i + 1)) f.write(response.content) print("已保存第 %d 个网站的图片" % (i + 1)) i += 1 else: with open("./" + dir_name_real + "/" + str(re.search(patten, website).group(2)) + ".jpeg", "wb") as f: print("正在保存第 %d 个网站的图片" % (i + 1)) f.write(response.content) print("已保存第 %d 个网站的图片" % (i + 1)) i += 1 except: website = "" break; else: website = "" #row+=1 break; row+=1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)