LoginSignup
1
0

More than 3 years have passed since last update.

Twitter APIでの日付指定

Last updated at Posted at 2020-12-23

はじめに

Twitter APIで日付指定をしたくなった。

環境

・Windows 10
・Python3.8
・Twitter API v1.1

目次

  1. 問題
  2. 結論
  3. 参考文献

問題

"q="の後に
since:とかuntil:
をつけてると、エラーが返ってくる

import json
from requests_oauthlib import OAuth1Session

ck = 'xxxxxxxxxxxxxxxxxxxxxxxxx' # コンシューマーキー
cs = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # コンシューマーシークレット
at = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # アクセストークン
ats = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # アクセストークンシークレット

twitter = OAuth1Session(ck, cs, at, ats)#OAuth認証


API_URL = "https://api.twitter.com/1.1/search/tweets.json?q="
keyword = "ハロ -RT since:2020-12-20_00:00:00_JST until:2020-12-21_00:00:00_JST"
type = "resent"
count = 50
url = API_URL + keyword + "&result_type=" + type \
+ "&count=" + str(count) + "&exclude_replies=True"
response = twitter.get(url)

response_data = json.loads(response.text)
# response_data
{
  "errors": [
    {
      "code": 32,
      "message": "Could not authenticate you."
    }
  ]
}

結論

keyword = "ハロ%20-RT520until%3A2020-12-23%20since%3A2020-12-22%20"
で成功

余談

Twitter APIの仕様上、一週間以内のツイートしか検索できないらしい。

参考

Twitter API リファレンス

1
0
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
1
0