python
1import tweepy 2from time import sleep 3import shutil 4import json 5import requests 6#tweepyの認証は省略します。 7 8def friends_ids_fanc(): 9 friends_ids_fanc_var = [] 10 for i,page in enumerate(tweepy.Cursor(api.friends_ids,id=user_).pages(2)): 11 for j,userID in enumerate(page): 12 friends_ids_fanc_var.append(userID) 13 return friends_ids_fanc_var 14 15friends_ids_var = friends_ids_fanc() 16print(friends_ids_var) 17#--------------------------------------------------------------------------------------------------------------------- 18def dispatching_func(friends,ids,N,Img_url): 19 if ids in friends: 20 #ここはあまり関係ないと考えられる 21 for i in range(N): 22 print(N) 23 else: 24 #ここが関係してると考えられる 25 for i in range(N): 26 imgurl = Img_url 27 print(imgurl) 28 imgdata = requests.get(imgurl) 29 filename = imgurl.split('/')[-1] 30 print(filename) 31 32 with open(filename,mode='wb') as f: 33 f.write(imgdata.content) 34 35 a = "/mnt/hdd1/twitter/no_name/"+filename 36 print(a) 37 shutil.move(a,'no_name') 38 39 40 41#---------------------------------------------------------------------------------------------- 42friends_ids_fanc() 43searchs = tweepy.Cursor(api.user_timeline, q = user_, include_entities = True, tweet_mode = 'extended').items(3200) 44for search in searchs: 45 n=0 46 ''' 47 print(i01) 48 print(json.dumps(search._json, indent=2)) 49 print('~'*30) 50 ''' 51 try: 52 for number in search.entities.get("user_mentions",{}): 53 friemds_number = number["id"] 54 friends_name = number["screen_name"] 55 print(str(friemds_number)) 56 print(friends_name) 57 except: 58 pass 59 try: 60 if hasattr (search,"extended_entities"): 61 for media in search.extended_entities.get("media",[]): 62 media_id = media["id"] 63 img_url = media["media_url"] 64 n=n+1 65 elif hasattr (search,'retweeted_status'): 66 medias = search.retweeted_status 67 for media in medias.extended_entities.get("media",[]): 68 media_id = media["id"] 69 img_url = media["media_url"] 70 n=n+1 71 dispatching_func(friends_ids_var,friemds_number,n,img_url) 72 n=0 73 except: 74 pass 75 tweet_id = search.id 76 try: 77 api.create_favorite(id=tweet_id) 78 except: 79 pass 80 try: 81 api.unretweet(id=tweet_id) 82 except: 83 pass
ubuntu20.04を使って上記のプログラムを実行してtwitterから画像をDLして特定のフォルダに保存しようとするができないのでどのようにしたら改善しますか?
回答1件
あなたの回答
tips
プレビュー