python:有没有根据EXCEL中的共享目录图片地址,批量获取图片保存到本地的方法

img

如上图,“FileName”这列全是共享目录下的图片地址的链接,想通过这列的地址,获取图片,并保存到本地;

使用过根据url的方法获取保存,报MissingSchema: Invalid URL错误,有没有其他办法可以批量获取

img

import os
import shutil
import xlwings as xw
import re


app=xw.App(visible=False,add_book=False)
app.display_alerts = False
app.screen_updating = False
app.screen_updating = False

# filepath是你表格文件的路径
filepath = ''
wb = app.books.open(filepath)
sht = wb.sheets[0] # 0表示第一个sheet

lastcell = sht.used_range.last_cell
end = lastcell.row # 获取已经使用单元格的行数
start = 1 # 从第几行开始,行号
list = sht.range('B{}:B{}'.format(start,end)).value # 获取地址列表

wb.close()
app.quit()

dir = os.path.exists(r'./下载')
if not dir:
    os.makedirs('./下载')
for url in list:
    file = os.path.exists(url)
    if file:
        old = url
        name =re.match(r'^.*\\(.+)$',url)
        new = r'./下载/{}'.format(name)
        shutil.copyfile(old, new)
    else:
        print('文件不存在')

你的需求应该就是这个吧?

看着没什么问题,我可以写

图片中网址不完整,缺少schema,在前面添上:http:或者https:即可。用requests.get,就可以获取图片了。
如有帮助请点击采纳。

import os

import pandas as pd
import requests

headers = {}
user_agent = r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
headers['User-Agent'] = user_agent
aim_dir = 'output/'
if not os.path.exists(aim_dir):
    os.mkdir(aim_dir)

example_list = ['\\\\img.netbian.com/file/2021/0908/a2497de931841a04297cdf74e0d68f52.jpg']
# input_excel = pd.read_excel('筛选数据.xlsx')
# for item in input_excel['FileName']:
for item in example_list:
    item = item.replace('\\', '/')
    print(item)
    img = requests.get('http:' + item, headers=headers)
    print(aim_dir + item.split('/')[-1].split('.')[0])
    with open(aim_dir + item.split('/')[-1].split('.')[0] + '.bmp', 'wb') as f:
        f.write(img.content)

大概是这种感觉,把读取替换为被注释掉的部分即可
有帮助望采纳

灵活运用os还有xlrd就OK了,用os.chdir()来转换地址并且后续储存图片,用xlrd来读取你的excl里面的图片地址,有用的话点一下采纳,如果你的地址是网站地址的话还需要用requests,来获取,最后.content来储存图片就OK了

把共享文件夹映射到本地磁盘,然后试试本地访问,就是直接用 open 打开文件


你可以看看博客,这个是复制共享文件夹的全部内容,你可以改成你要的图片内容
https://www.bbsmax.com/A/amd0ajmzge/
如果可行,望采纳!

你要先确认下,手动访问图片是这么操作的,然后再用python模型人的行为,帮你实现自动化

之前用python写过,我把url和名字单独放一个文本,然后请求下载,效果还可以,就是速度太慢,不是多线程,我直接挂服务器跑的