python3 使用多线程利用Instaloader从instagram上下载上数据

1.上次做了一个利用Instaloader下载数据的示例,我发现这方面的教程很少,由于单线程下载速度慢,我这里利用多线程来加速下载数据,注意worker的数量不要设置太多,不然会报一下错误,错误信息为:

JSON Query to explore/locations/245942146/: 429 Too Many Requests

2.如果想知道hashtags.txt里面是啥,请参考前面的博客:https://blog.csdn.net/w5688414/article/details/85061680

还是一样前提是租一台国外的虚拟主机,国内下载不了,以下为多线程下载,文件名为demo.oy

from threading import Thread
from time import time, sleep
from queue import Queue
from datetime import datetime
import instaloader


    
# for HASHTAG in hashtags:
#     try:
#         posts = L.get_hashtag_posts(HASHTAG)
#         count=0
#         print(HASHTAG)
#         for post in posts:
#             if(post.is_video):
#                 continue
#             if(count==1000):
#                 break
#         #         print(post.date)
#             L.download_post(post, target='#'+HASHTAG)
#             count+=1
#     except Exception as e:
#         print(e)
L = instaloader.Instaloader()
def download_tweets(HASHTAG):
    try:
        posts = L.get_hashtag_posts(HASHTAG)
        count=0
        print(HASHTAG)
        for post in posts:
            if(post.is_video):
                continue
            if(count==1000):
                break
        #         print(post.date)
            L.download_post(post, target='#'+HASHTAG)
            count+=1
    except Exception as e:
        print(e)
        fp = open("error.txt", "a")
        fp.write(str(e)+"\n")
        fp.close()


class DownloadWorker(Thread):

    def __init__(self, queue,sleep=1):
        Thread.__init__(self)
        self.queue = queue
        self.numPicrures=0
        self.sleep = sleep

    def run(self):
        while True:
            # Get the work from the queue and expand the tuple
            item = self.queue.get()
            if item is None:
                break
            # print(imageUrl)
            download_tweets(item)
            self.queue.task_done()
            sleep(self.sleep)

if __name__ == "__main__":

    with open('hashtags.txt', encoding="utf-8") as f:
        examples=f.readlines()

    hashtags=[]
    for item in examples:
        hashtag=item.strip().replace('#','')
        hashtags.append(hashtag)
        
    ts = time()
    queue = Queue()
    for x in range(5):
        worker = DownloadWorker(queue,2)
        # Setting daemon to True will let the main thread exit even though the
        # workers are blocking
        worker.daemon = True
        worker.start()

    for hashtag in hashtags:
        queue.put(hashtag)
    queue.join()
    print('Took {}s'.format(time() - ts))

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值