gpt4 book ai didi

python - 如何将图片下载到 Windows 上的特定文件夹位置?

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:05 26 4
gpt4 key购买 nike

我有这个脚本可以从给定的网址下载所有图像:

from selenium import webdriver
import urllib


class ChromefoxTest:

def __init__(self,url):
self.url=url
self.uri = []

def chromeTest(self):
# file_name = "C:\Users\Administrator\Downloads\images"
self.driver=webdriver.Chrome()
self.driver.get(self.url)
self.r=self.driver.find_elements_by_tag_name('img')
# output=open(file_name,'w')

for i, v in enumerate(self.r):
src = v.get_attribute("src")
self.uri.append(src)
pos = len(src) - src[::-1].index('/')
print src[pos:]
self.g=urllib.urlretrieve(src, src[pos:])
# output.write(src)
# output.close()


if __name__=='__main__':
FT=ChromefoxTest("http://imgur.com/")
FT.chromeTest()

我的问题是:如何制作此脚本以将所有图片保存到我的 Windows 机器上的特定文件夹位置?

最佳答案

您需要指定要保存文件的路径。这在 the documentation for urllib.urlretrieve 中有解释。 :

方法是:urllib.urlretrieve(url[, filename[, reporthook[, data]]])。文档说:

The second argument, if present, specifies the file location to copy to (if absent, the location will be a tempfile with a generated name).

所以...

urllib.urlretrieve(src, 'location/on/my/system/foo.png')

将图像保存到指定的文件夹。

此外,请考虑查看 documentation for os.path .这些函数将帮助您操作文件名和路径。

关于python - 如何将图片下载到 Windows 上的特定文件夹位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45782463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com