0

I want to extract Japanese tweet text using the tweet API. Are there some sample code about this problem?

I just want extract Japanese tweets that are not using a keyword.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tktktk0711
  • 1,656
  • 7
  • 32
  • 59

1 Answers1

0

You can add 'lang' key when you make a query to restrict the language.
According to the document, it says "Restricts tweets to the given language, given by an ISO 639-1 code. Language detection is best-effort."

It leads to something like this:

import json
from requests_oauthlib import OAuth1Session

tw = OAuth1Session("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET")

url = "https://api.twitter.com/1.1/search/tweets.json"

params = {'q' : "keyword", 'count' : 100, 'lang' : 'ja'}

while True:
    req = tw.get(url, params=params)

    if req.status_code == 200:
        tweets = json.loads(req.text)
        for tweet in tweets['statuses']:
            print(tweet['text'])
            print("<<<<<<<<<<<<<<<<<<<")
shogo2022
  • 36
  • 1
  • 5
  • thanks for your answer. Could you tell me what's tw? – tktktk0711 Sep 23 '17 at 02:04
  • hi @shogo2022 how to get more tweets such as 10000. After I set the count:10000, but I can only get about 100 tweets. How to solve this issue. – tktktk0711 Sep 23 '17 at 03:35
  • @tktktk0711 it might be [rate limit](https://dev.twitter.com/rest/public/rate-limiting) on twitter search API, you can check how rate limit is calculated in [How rate limit works in twitter in search API](https://stackoverflow.com/questions/21305547/how-rate-limit-works-in-twitter-in-search-api). Search API only returns tweets from past 7 days, but I believe there are way more than 10K tweets in Japanese. – shogo2022 Sep 23 '17 at 06:45