Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在编号时下载图像(使用多处理)_Python_Python 3.x - Fatal编程技术网

Python 如何在编号时下载图像(使用多处理)

Python 如何在编号时下载图像(使用多处理),python,python-3.x,Python,Python 3.x,我想按列表的顺序保存文件。(比如bbb.jpg->001.jpg,aaa.jpg->002.jpg…) 由于按字母顺序排列,文件未按我的要求保存。(如aaa.jpg、bbb.jpg、ccc.jpg……) 还有一种按时间顺序对文件进行排序的方法,但也不可能使用多处理 因此,我的问题是,如何才能以我想要的顺序或名称保存文件 这是我的密码 从urllib.request导入请求,urlopen 导入urllib.request 导入操作系统 导入操作系统路径 进口稀土 导入时间 来自多处理导入池 导入

我想按列表的顺序保存文件。(比如bbb.jpg->001.jpg,aaa.jpg->002.jpg…)

由于按字母顺序排列,文件未按我的要求保存。(如aaa.jpg、bbb.jpg、ccc.jpg……)

还有一种按时间顺序对文件进行排序的方法,但也不可能使用多处理

因此,我的问题是,如何才能以我想要的顺序或名称保存文件

这是我的密码

从urllib.request导入请求,urlopen
导入urllib.request
导入操作系统
导入操作系统路径
进口稀土
导入时间
来自多处理导入池
导入多处理
从functools导入部分
mylist=['https://examsite.com/bbb.jpg',
'https://examsite.com/aaa.jpg',
'https://examsite.com/ddd.jpg',
'https://examsite.com/eee.jpg',
'https://examsite.com/ccc.jpg']
def image_URL_下载(路径,html):
originNames=(f“{html}”.split)('/')[-1]
PathandNames=(路径+str(原始名称))
req=urllib.request.request(html,标题={'User-Agent':'Mozilla/5.0'})
urlopen=request.urlopen(req.read)()
打开(路径和名称,'wb')作为保存文件2:
savefile2.write(urlopen)
打印(f“下载{originNames}”)
如果名称=“\uuuuu main\uuuuuuuu”:
开始=时间。时间()
路径='/向下'
池=多处理。池(进程=4)
img\u down=部分(图像URL\u下载,路径)
pool.map(img_down,mylist)
pool.close()
pool.join()
打印(“完成!时间:”,time.time()-start)

下面是一个完整的示例,它从Wikimedia commons图像中获取了一组图像(这里是缩略图)。它保存编号为
000.jpg
001.jpg
,等等(在
/tmp
中,当然可以根据需要进行调整)。奖励:下载过程中会显示动画进度条,礼节:

导入请求
从bs4导入BeautifulSoup
从urllib.parse导入urljoin
从concurrent.futures导入ThreadPoolExecutor
从TQM导入TQM
tld=https://commons.wikimedia.org'
url='/wiki/Category:Images'
soup=BeautifulSoup(requests.get(urljoin(tld,url)).content)
imglist=[x.get('src')表示汤中的x.find_all('img',src=True)]
imglist=[urljoin(tld,x)表示imglist中的x,如果x.endswith('.jpg')]
def加载img(i_url):
i、 url=i\u url
img=requests.get(url.content)
以open(f'/tmp/{i:03d}.jpg',wb')作为f:
f、 写入(img)
返回真值
def load_all(imglist):
使用ThreadPoolExecutor()作为执行器:
结果=列表(TQM)(
executor.map(load\u img,enumerate(imglist)),
总计=镜头(imglist),单位为“图像”)
返回结果
结果=全部加载(imglist)

和?你的问题是什么?请阅读此场景并提供一个示例,多线程将执行得更好