前提・実現したいこと
Twitterのツイート検索APIを使って画像を自動収集しようとしています。
以下、参考にしている記事です。
参考記事
発生している問題・エラーメッセージ
$ pip install imgutilで、パッケージはインストールしたのですが、
以下のエラーが出ており、どの様に修正したらよいかわからず困っています。
bash
1$python tweetImage.py 2Traceback (most recent call last): 3 File "tweetImage.py", line 7, in <module> 4 import imgutil 5 File "/Users/username/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages/imgutil/__init__.py", line 3, in <module> 6 from imgoptimize import imgoptimize 7ModuleNotFoundError: No module named 'imgoptimize'
該当のソースコード
ソースコードは記事のままです。
tweetImage.py
python
1# -*- coding:utf-8 -*- 2 3import requests 4import requests_oauthlib 5import json 6import math 7import imgutil 8 9# image save path 10directory = "/path/to/img" 11imgutil.mkdir(directory) 12 13url = "https://api.twitter.com/1.1/search/tweets.json" 14 15# parameters 16query = "キーワード" 17lang = "ja" 18result_type="recent" # 最新のツイートを取得 19count = 100 # 1回あたりの最大取得ツイート数(最大100) 20max_id = '' 21 22total_count = 1000 # 取得ツイートのトータル 23offset = math.floor(total_count/count) # ループ回数 24 25# oauthの設定 26consumer_key = "xxxxxxxxxx" 27consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 28access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 29access_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 30oauth = requests_oauthlib.OAuth1(consumer_key,consumer_secret,access_token,access_secret) 31 32for i in range(offset): 33 params = {'q':query,'lang':lang,'result_type':result_type,'count':count,'max_id':max_id} 34 r = requests.get(url=url,params=params,auth=oauth) 35 json_data = r.json() 36 for data in json_data['statuses']: 37 # 最後のidを格納 38 max_id = str(data['id']-1) 39 if 'media' not in data['entities']: 40 continue 41 else: 42 for media in data['entities']['media']: 43 if media['type'] == 'photo': 44 image_url = media['media_url'] 45 try: 46 imgutil.download_img(directory,image_url) 47 except Exception as e: 48 print("failed to download image at {}".format(image_url)) 49 print(e) 50 continue
試したこと
エラー文を受けて、ないと言われたパッケージのインストールを試みましたが、
こちらもエラーでした。
bash
1$pip install imgoptimize 2Collecting imgoptimize 3 Could not find a version that satisfies the requirement imgoptimize (from versions: ) 4No matching distribution found for imgoptimize
###ご回答をうけて試したことと別のエラー
mgutilのファイルをGitHubからインストールした後、 tweetImage.pyを実行したところ、
また別のモジュールが無いようでした。
bash
1$python tweetImage.py 2Traceback (most recent call last): 3 File "tweetImage.py", line 7, in <module> 4 import imgutil 5 File "/Users/username/Desktop/assignment3/imgutil.py", line 6, in <module> 6 import sha3 7ModuleNotFoundError: No module named 'sha3'
このエラーは$ pip install pysha3で消えましたが、
別のエラーが複数表示され、どのように修正すれば良いかわからずに困っています。
bash
1$ python tweetImage.py 2Traceback (most recent call last): 3 File "tweetImage.py", line 11, in <module> 4 imgutil.mkdir(directory) 5 File "/Users/username/Desktop/assignment3/imgutil.py", line 11, in mkdir 6 os.makedirs(path) 7 File "/Users/username/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/os.py", line 210, in makedirs 8 makedirs(head, mode, exist_ok) 9 File "/Users/username/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/os.py", line 210, in makedirs 10 makedirs(head, mode, exist_ok) 11 File "/Users/username/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/os.py", line 220, in makedirs 12 mkdir(name, mode) 13PermissionError: [Errno 13] Permission denied: '/path'
補足情報(FW/ツールのバージョンなど)
Python 3.6.3
Mac OS High Sierra
ターミナル バージョン2.8.2

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/30 04:00
退会済みユーザー
2018/07/01 04:50
退会済みユーザー
2018/07/02 00:07
退会済みユーザー
2018/07/02 00:10
2018/07/02 07:32 編集
退会済みユーザー
2018/07/02 09:56