1.遇到了这个问题
cur_page = queue.pop(0)
整段代码是这样的
def main():
create_dir('pic') # 创建主文件夹
queue = [i for i in range(1, 72)] # 构造url链接的页码列表
threads = []
while len(queue) > 0:
for thread in threads:
if not thread.is_alive():
threads.remove(thread)
while len(threads) < 5 and len(queue) > 0: # 最大线程数设置为 5
cur_page = queue.pop(0) # 移除列表中第一个元素,并返回该元素的值
url = 'http://meizitu.com/a/more_{}.html'.format(cur_page)
thread = threading.Thread(target=execute, args=(url,)) # 传入excute函数
thread.setDaemon(True)# 设置线程为守护线程
thread.start()
print('{}正在下载{}页'.format(threading.current_thread().name, cur_page))
threads.append(thread)# 把线程从线程列表中删除
2.用法
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
cur_page = queue.pop(0)
该处是移除列表中第一个元素,并返回该元素的值
cur_page = queue.pop() # 默认不传值的时候
默认为 index=-1,删除最后一个列表值。
列表操作案例
本文通过一个具体实例展示了如何使用Python中的列表及其pop方法来实现网页抓取任务中的URL管理。通过构造URL列表并利用多线程下载图片,文章详细解释了列表pop方法的应用,即从列表中移除并获取指定位置的元素。
836

被折叠的 条评论
为什么被折叠?



