<!-Description the beginning and outline-> I want to specify the date with the Twitter API.
<!-Development environment-> ・ Windows 10 -Python3.8 ・ Twitter API v1.1
<!-Edit title and anchor name->
After "q =" since: or until: If you turn on, an error will be returned
import json
from requests_oauthlib import OAuth1Session
ck = 'xxxxxxxxxxxxxxxxxxxxxxxxx' #Consumer key
cs = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Consumer secret
at = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Access token
ats = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #Access token secret
twitter = OAuth1Session(ck, cs, at, ats)#OAuth authentication
API_URL = "https://api.twitter.com/1.1/search/tweets.json?q="
keyword = "Halo-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 = "Halo% 20-RT520until% 3A2020-12-23% 20since% 3A2020-12-22% 20" Successful
Due to the specifications of the Twitter API, it seems that only tweets within a week can be searched.
Recommended Posts