我正在使用API,以确定的类别返回我语言的文章。对于60 minutes
的每个时间间隔,此API会限制100次调用。
我正在尝试在此100 calls/hour
窗口中下载100篇文章。
我选择这样做的方式(为了避免直接下载100个文件)将随机选择0到60 * 60之间的100个数字。这些是我发出请求的秒数。每次等待next - current
秒时,对它们进行排序并作为我的函数在数组中前进。
为此,我创建了一个函数,生成0到3600 (3600 seconds = 1 hour)
之间的100个唯一数字并对它们进行排序:
def generateTimes():
download_wait_times = random.sample(xrange(3600), 100) #Generates 100 unique numbers between 0 and 3600( 3600 seconds = 1 hour).
download_wait_times.sort() #Sorts the numbers.
return download_wait_times
download_wait_times的内容:
>>> print like_times
[15, 43, 51, 85, 98, 130, 157, 159, 181, 233, 336, 371, 390, 456, 458, 481, 488, 503, 539, 594, 637, 650, 698, 795, 837, 851, 963, 1001, 1020, 1027, 1045, 1081, 1163, 1187, 1191, 1239, 1316, 1405, 1585, 1625, 1657, 1658, 1753, 1807, 1881, 1902, 1933, 1941, 2001, 2022, 2050, 2132, 2167, 2169, 2220, 2252, 2314, 2386, 2391, 2392, 2485, 2500, 2505, 2558, 2584, 2651, 2656, 2657, 2662, 2721, 2730, 2790, 2796, 2872, 2902, 2913, 2935, 2941, 2951, 2974, 3045, 3047, 3066, 3128, 3163, 3211, 3218, 3316, 3354, 3361, 3397, 3424, 3428, 3436, 3492, 3498, 3534, 3541, 3544, 3552]
然后,这个函数循环播放我的文章列表,下载一篇文章并按照我的公式睡眠,next
等待时间minus
current
等待时间:
def myFunction():
for media in download_list[0]:
api.download_file(media) #Downloads the article
downloaded += 1
print("Downloaded file: %s. " % media + "Total number of files downloaded: %d. " % downloaded)
print "Sleeping for %d seconds." % download_wait_times[time_count]
time_count =+ 1
time.sleep(download_wait_times[time_count+1] - download_wait_times[time_count]) #next minus current
当我运行我的脚本时,这是输出:
>>> execfile('test.py')
downloaded 905462027415412779. Number of downloaded articles: 1.
Sleeping for 15 seconds.
downloaded 905462008013920125. Number of downloaded articles: 2.
Sleeping for 43 seconds.
downloaded 905445715499119709. Number of downloaded articles: 3.
Sleeping for 43 seconds.
downloaded 905461942110434199. Number of downloaded articles: 4.
Sleeping for 43 seconds.
downloaded 905461981722457386. Number of downloaded articles: 5.
Sleeping for 43 seconds.
downloaded 905461979140234589. Number of downloaded photos: 6.
Sleeping for 43 seconds.
而不是在下一段时间内睡觉,而不是51
,它会在43
秒内停留,我无法弄清楚原因。
我做错了什么?
答案 0 :(得分:3)
time_count =+ 1
这一定是;
time_count += 1
第一个等于time_count = 1
。第二个是time_count = time_count + 1
。
在第一个实际上你说的是variable time_count equals to positive one (+1)
。这是他们之间的区别。你可以记住这样的话;
例如,如果你写这个,你想减少一个变量;
x = -1
您可以轻松地看到它重新定义变量。所以你要记住,你必须像写一样;
x -= 1
所以当你增加一个变量时,你必须这样做,如果你写这个;
x = +1
正如您在第一个示例中看到的那样,它重新定义了它。有效的是;
x += 1