使用instaloader下载instagram上的视频很慢,写个多线程程序可有效解决这个问题
    这里是用post的shortcode下载,你可以改成用其他方式下载,具体请看instaloader官方文档

    1. from threading import Thread
    2. from time import time, sleep
    3. from queue import Queue
    4. from datetime import datetime
    5. import instaloader
    6. from shortcodes import shortcodes
    7. from instaloader import Post
    8. L = instaloader.Instaloader(
    9. compress_json=False
    10. )
    11. L.login(user='', passwd='') # 填入你的ins用户名和密码
    12. # L.load_session_from_file('')
    13. def download_tweets(shortcode):
    14. try:
    15. post = Post.from_shortcode(L.context, shortcode)
    16. if post.mediacount >= 1:
    17. L.download_post(post, target='start here') # target是目标文件夹
    18. except Exception as e:
    19. print(e)
    20. fp = open("error.txt", "a")
    21. fp.write(str(e)+"\n")
    22. fp.close()
    23. class DownloadWorker(Thread):
    24. def __init__(self, queue, sleep=1):
    25. Thread.__init__(self)
    26. self.queue = queue
    27. self.numPicrures = 0
    28. self.sleep = sleep
    29. def run(self):
    30. while True:
    31. # Get the work from the queue and expand the tuple
    32. item = self.queue.get()
    33. if item is None:
    34. break
    35. # print(imageUrl)
    36. download_tweets(item)
    37. self.queue.task_done()
    38. sleep(self.sleep)
    39. if __name__ == "__main__":
    40. ts = time()
    41. queue = Queue()
    42. for x in range(5):
    43. worker = DownloadWorker(queue, 2)
    44. # Setting daemon to True will let the main thread exit even though the
    45. # workers are blocking
    46. worker.daemon = True
    47. worker.start()
    48. for shortcode in shortcodes:
    49. queue.put(shortcode)
    50. queue.join()
    51. print('Took {}s'.format(time() - ts))