【发布时间】:2020-06-03 20:36:09
【问题描述】:
我对使用 Python 还是很陌生。我一直在尝试设置一个非常基本的网络爬虫来帮助加快我的工作日,它应该从网站的某个部分下载图像并保存它们。
我有一个 url 列表,我正在尝试使用 urllib.request.urlretrieve 下载所有图像。
输出位置 (savepath) 会更新,因此它会将文件夹中当前的最高数字加 1。
我尝试了很多不同的方法,但urlretrieve 只保存列表中最后一个 url 中的图像。有没有办法下载url列表中的所有图片?
to_download=['url1','url2','url3','url4']
for t in to_download:
urllib.request.urlretrieve(t, savepath)
这是我每次尝试用来更新savepath 的代码
def getNextFilePath(photos):
highest_num = 0
for f in os.listdir(photos):
if os.path.isfile(os.path.join(photos, f)):
file_name = os.path.splitext(f)[0]
try:
file_num = int(file_name)
if file_num > highest_num:
highest_num = file_num
except ValueError:
'The file name "%s" is not an integer. Skipping' % file_name
output_file = os.path.join(output_folder, str(highest_num + 1))
return output_file
【问题讨论】:
-
你需要在for循环中更新保存路径
-
也许可以用完整的代码问另一个问题。不知道 2 是如何相互关联的。
标签: python python-3.x web-scraping urllib urlretrieve