我正在尝试从URL列表下载图像。每个URL包含一个带有jpeg信息的txt文件。除了文件夹号的增量更改外,URL都是统一的。以下是示例网址的
最小值:https://marco.ccr.buffalo.edu/data/train/train-00001-of-00407
上限:https://marco.ccr.buffalo.edu/data/train/train-00407-of-00407
我想阅读每个URL,并将它们的输出存储到另一个文件夹。我一直在研究请求python库来执行此操作,但我想知道如何遍历URL并本质上写我的循环以递增URL中的该数字。如果我滥用该术语,请提前道歉。谢谢!
// Highlighted search for numbers
NSString *phoneString = @"+381 60 0000000";
NSString *searchString = @"38160";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:searchString options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;
答案 0 :(得分:1)
您可以生成这样的网址并为每个网址执行获取
for i in range(1,408):
url = "https://marco.ccr.buffalo.edu/data/train/train-" + str(i).zfill(5) + "-of-00407"
print (url)
还使用文件名中的变量来保留每个变量的不同副本。例如,使用此
with open("data" + str(i) + ".txt",'wb') as f:
总体代码可能看起来像这样(不完全是这样)
import requests
for i in range(1,408):
url = "https://marco.ccr.buffalo.edu/data/train/train-" + str(i).zfill(5) + "-of-00407"
r = requests.get(url)
# you might have to change the extension
with open("data" + str(i).zfill(5) + ".txt",'wb') as f:
f.write(r.content)