python读取excel下载图片
python读取excel下载图片#!/usr/bin/python# encoding: utf-8from __future__ import print_functionimport osimport pandas as pdimport requestsimport numpy as npimport _threadheaders = {'User-Agent': 'Mozilla/5.0
·
python读取excel下载图片
#!/usr/bin/python
# encoding: utf-8
from __future__ import print_function
import os
import pandas as pd
import requests
import numpy as np
import _thread
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
def downloadImg(parent_path,imgpath, headers):
baseUrl1 = u'http://图片域名地址1'
baseUrl2 = u'http://图片域名地址2'
imgurl = imgpath
imgname = imgpath.split('/')[-1]
if not os.path.exists(parent_path):
os.makedirs(parent_path)
imglocalpath = os.path.join(parent_path, imgname)
img = requests.get(baseUrl1+imgurl, headers)
if img.status_code == 404:
img = requests.get(baseUrl2 + imgurl, headers)
if img.status_code == 404:
return "error"
with open(imglocalpath, 'wb') as f:
f.write(img.content)
return imglocalpath
# 读取excel数据
data = None
def readXls():
io = r'D:/Python/pythonwork1/test1/docTest/dy.xlsx'
data = pd.read_excel(io.replace('\xe2\x80\xaa',''), sheet_name='Sheet1',skiprows=0,keep_default_na=False)
return data
if __name__ == '__main__':
data = readXls()
data1 = pd.DataFrame(columns = data.columns)
for i in data.index:
imgpath =data.loc[i]
parent_path = os.path.join("D:/Python/pythonwork1/test1/docTest/ht",data.loc[i].car_business_id,data.loc[i].doc_name)
if downloadImg(parent_path,data.loc[i].url,headers) == "error":
# 写入读取不到的信息
data1 = data1.append(data.loc[i])
# 写如读取不到的信息
io1 = r'D:/Python/pythonwork1/test1/docTest/dy404.xlsx'
data1.to_excel(io1,"not_found")
更多推荐
已为社区贡献3条内容
所有评论(0)