twitterAPIを用いたtimelineの取得
from requests_oauthlib import OAuth1Session import json from urllib import request keys = { "CK":'xxxxx', "CS":'xxxxx', "AT":'xxxxx', "AS":'xxxxx', } sess = OAuth1Session(keys["CK"], keys["CS"], keys["AT"], keys["AS"]) url = "https://api.twitter.com/1.1/statuses/home_timeline.json" params = {"count":200, #ツイートを最新から何件取得するか(最大200件) "include_entities" : 1, #エンティティ(画像のURL等)をツイートに含めるか "exclude_replies" : 1, #リプライを含めるか } req = sess.get(url, params=params) timeline = json.loads(req.text) print(timeline[0]["user"])
自分のツイートを取得したところこのようになった.
{'id': 863468411120005120, 'id_str': '863468411120005120', 'name': '🐈', 'screen_name': 'tristana_chan', 'location': '',\ 'description': '有益なことは呟かないため鍵垢にしていますがフォローお気軽に', 'url': None,'entities': {'description': {'urls': []}}, \ 'protected': True, 'followers_count': 40, 'friends_count': 395, 'listed_count': 0, 'created_at': 'Sat May 13 18:58:06 +0000 2017',\ 'favourites_count': 2884, 'utc_offset': -25200, 'time_zone': 'Pacific Time (US & Canada)', 'geo_enabled': False, 'verified': False,\ 'statuses_count': 3281, 'lang': 'ja', 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False,\ 'profile_background_color': 'F5F8FA', 'profile_background_image_url': None, 'profile_background_image_url_https': None,\ 'profile_background_tile': False, 'profile_image_url': 'http://pbs.twimg.com/profile_images/899736954631094272/JtKhe4RD_normal.jpg',\ 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/899736954631094272/JtKhe4RD_normal.jpg', 'profile_link_color': '1DA1F2',\ 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True,\ 'has_extended_profile': False, 'default_profile': True, 'default_profile_image': False, 'following': False, 'follow_request_sent': False, 'notifications': False,\ 'translator_type': 'none'}
今後有効そうなkey値だけまとめておく.
key | value |
name | ユーザーの名前 |
screen_name | ユーザーID(@以降) |
location | 住んでるところ(設定していない場合は"") |
description | ユーザーの紹介文 |
url | ユーザーが設定しているURL |
protected | Trueならば鍵垢 |
followers_count | フォロワー数 |
friends_count | フォロー数 |
listed_count | 登録されているリスト数 |
created_at | アカウントが作られた日 |
lang | 使用している言語(日本語なら"ja") |
profile_image_url | プロフィールのイメージのURL(http) |
profile_image_url_https | プロフィールのイメージのURL(https) |
タイムライン上のプロフィール画像を取得する
from requests_oauthlib import OAuth1Session import json from urllib import request import subprocess keys = { "CK":'xxxxx', "CS":'xxxxx', "AT":'xxxxx', "AS":'xxxxx', } sess = OAuth1Session(keys["CK"], keys["CS"], keys["AT"], keys["AS"]) url = "https://api.twitter.com/1.1/statuses/home_timeline.json" params = {"count":200, #ツイートを最新から何件取得するか(最大200件) "include_entities" : 1, #エンティティ(画像のURL等)をツイートに含めるか "exclude_replies" : 1, #リプライを含めるか } req = sess.get(url, params=params) timeline = json.loads(req.text) #タイムライン上にいる100人の人のイメージアイコンをダウンロードする dir_path = "./profile_images/" url_set = set() for tweet in timeline: #プロフィールの画像URLを取得 image_url = tweet["user"]["profile_image_url"] #画像の拡張子を取得 image_ex = image_url.split(".")[-1].replace("jpeg","jpg")#jpg, png, gif #ユーザの名前を取得 user_id = tweet["user"]["name"].replace(" ","_") if image_url not in url_set: url_set.add(image_url) file_name = user_id + "." + image_ex args = ["wget",image_url,"-O",dir_path+file_name] subprocess.call(args) else: pass
のようにするとタイムライン上のアカウントのプロフィール画像を取得できる.
(個人アカウントはモザイク済み)