LoginSignup
2
7

More than 3 years have passed since last update.

Python3 で Twitter をサーチ

Last updated at Posted at 2017-05-13

次のプログラムは、"python" というキーワードのサーチの例です。

search_twitter.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   search_twitter.py
#
#                        Apr/26/2019
# --------------------------------------------------------------------
import sys
import json
from requests_oauthlib import OAuth1Session
from config_twitter import config_twitter_proc
# --------------------------------------------------------------------
def tweet_search(search_word):
    url = "https://api.twitter.com/1.1/search/tweets.json?"
    params = {
    "q": search_word,
    "lang": "ja",
    "result_type": "recent",
    "count": "5"
    }

    twitter = config_twitter_proc()
    responce = twitter.get(url, params = params)
    if responce.status_code != 200:
        print("Error code: %d" %(responce.status_code))
        return None
    tweets = json.loads(responce.text)
    return tweets
#
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_word = "#python"
tweets = tweet_search(key_word)
#
for tweet in tweets["statuses"]:
    tweet_id = tweet[u'id_str']
    text = tweet[u'text']
    created_at = tweet[u'created_at']
    user_id = tweet[u'user'][u'id_str']
    user_description = tweet[u'user'][u'description']
    screen_name = tweet[u'user'][u'screen_name']
    user_name = tweet[u'user'][u'name']
    print("tweet_id:", tweet_id)
    print("text:", text)
    print("created_at:", created_at)
    print("user_id:", user_id)
    print("user_desc:", user_description)
    print("screen_name:", screen_name)
    print("user_name:", user_name)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
config_twitter.py
# --------------------------------------------------------------------
#   config_twitter.py
#
#                   Apr/25/2019
# --------------------------------------------------------------------
import os
from dotenv import load_dotenv
from requests_oauthlib import OAuth1Session

# --------------------------------------------------------------------
def config_twitter_proc():
    dotenv_path = '.env'
    load_dotenv(dotenv_path)
    CONSUMER_KEY = os.environ.get("CONSUMER_KEY")
    CONSUMER_SECRET = os.environ.get("CONSUMER_SECRET")
    ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
    ACCESS_TOKEN_SECRET = os.environ.get("ACCESS_TOKEN_SECRET")
#
    twitter = OAuth1Session \
        (CONSUMER_KEY,CONSUMER_SECRET,ACCESS_TOKEN,ACCESS_TOKEN_SECRET)
#
    return twitter
# --------------------------------------------------------------------
.env
CONSUMER_KEY = '******'
CONSUMER_SECRET = '******'
ACCESS_TOKEN = '******'
ACCESS_TOKEN_SECRET = '******'

Arch Linux でライブラリのインストール

sudo pacman -S python-requests-oauthlib

次の環境で動作を確認しました。

$ uname -a
Linux iwata 5.6.13-arch1-1 #1 SMP PREEMPT Thu, 14 May 2020 06:52:53 +0000 x86_64 GNU/Linux

$ python --version
Python 3.8.3

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7