見出し画像

#D-TW03 ツイッターデータを集めてみる (Collecting twitter data)

(English follows)
前回はツイッターを使い倒してどんなことが書かれているのか、どんなデータを持っているのか、そしてデータの制約みたいなことを書きました。今回は実際にどうやってデータ収集部分を設計したのか、という部分を話したいと思います。変わらずこのシリーズでは実際のコードにはなるべく触れずに説明をしていこうと思っています。

  1. 検索コマンド

  2. クエリーパラメーター

  3. 次のページのデータ

ツイッターのデータを収集するに当たって重要なポイントは「検索コマンド」と「クエリーパラメーター」の2つになります。
検索コマンド
今回使うSearch APIというのはツイッターのキーワード検索ボックスを使用したのと同じ結果のデータを渡してくれるというAPIになります。そして調べてみるとキーワード検索には「コマンド」と呼ばれる、検索されるものを絞り込む方法があるのです。例えば「雪マウント from:Juntranet」と検索すると僕のツイートのなかだけから検索してくれたりしますし、「花王 -株」と入れると「花王」とツイートのなかから「株」と入ったツイートを省いてデータを返してくれます。なのでこれを使いこなすと、自分が欲しいデータに近づきやすくなると思われます。記事の一番下のReference (参考)というところに参考記事を載せておきました。言語を絞り込んだり、日付を指定したりとかもできるようです。

クエリーパラメーター
もう一つ今回のデータ収集のAPIを使うに当たってのポイントは、ツイッターの開発者ページ(ここ)でQuery parameterと呼ばれているものになります。これはざっくりいうとAPIを呼び出すにあたって「指定できる項目のリスト」みたいなものになります、、、と思う。APIを呼び出すに当たって、先程の検索コマンドなどを使って作った「検索ボックス」に入れるテキストや、もらえるデータの一覧から「どのデータがほしいか」の指定。あとは何件のデータを返してほしいかとか、特定のツイートより未来とか過去みたいな設定をしてデータを呼び出すことができます。たとえば、「〇〇で検索した結果をツイート内容と、画像のURLとアカウントIDとソース、Likeされた回数と一緒にください」といったことをここで指定します。*ソースというのはどのアプリやサービスからツイートされたかみたいなやつです。

次のページのデータ
さて上記2つでAPIを呼び出すとデータを取ることができるのですが(プログラムをかければ。。。)、最後に気をつけなければいけないのが、呼び出せるデータ数の制限になります。クエリーパラメーターには何件のデータ呼び出せるかというのを設定する項目があるのですがそれの最大が100件なのです。アプリで表示したりするためには十分な件数ですが、テキストを分析目的で使おうとすると全然足りません。例えば「新年の抱負」の分析(ここ)では数日で1万件程度のツイートが出てきました。
ここで活躍(?)するのがnext tokenと言われるものです。クエリーパラメーターに指定できるのですが、そこに何も入れないで100件を指定してデータを取得すると、100件のデータの最後にこのトークン(ID)がついてきます。これは「次のページ」に行くには、クエリーパラメーターのnext tokenにこのIDを入れて検索してね!という指定になるのです。毎回データを取得(リクエスト)する度にこのIDがついてくるので、そのIDをつけてリクエストするというのを繰り返すと1万件のツイートでも100回のリクエストで取れるという計算になります。

さて、ここまでで一旦ほしいツイートを、探して、集めるということができるようになった、、、気がするので次の回では、もう少しデータ収集と設計の話をしようと思います。ふぃー。やっぱり毎回長くなってしまうな。。。

The last article was about understanding tweet data by actually using twitter, what type of data twitter holds and limitation of getting twitter data. This article is about the next step, how we collect data from twitter. Again I will explain it without showing codes for non-programmers.

  1. Search command

  2. Query parameter

  3. Next page token

What you need to know about getting data from twitter are "Search command" and "Query parameter"
Search command
Search API which I use this time gives you data which is the same as the result of using keyword search box in twitter. Then keyword search actually has command function to let you make detail request to the search. For example, if you type "Snow from:Juntranet" to the search box, it give you the search result of "Snow" from My tweets". Also if you type "Kao -Stock", you will get the search result of "Kao" excluding "Stock" in tweets. So understanding search commands is important to get closer to data which you want to get. It allows you to suggest language or date etc too.

Query parameter
Another important thing to know is what is called "query parameter" in twitter developer page (here) if you want to use Search API to get tweet data… I think. When you call this API you need to make list of "settings" to request data. You have to add "Query text" which you made with search commands. Also you have to set which data you need to get from Response fields, how many tweets you want to get or you may suggest like "I want to get tweets older than the particular tweet. For example, this would be "I want to get the results of [New year resolution] with tweet text, URL for images, source and count of likes". * Source: the name of app/service which a tweet is made.

Next page token
Now you will be able to get data from API (still need to write a program), you also have be careful about how much data you can get by one request. You can get maximum only 100 tweets in one request. It would be enough to developers making an app but for you who want to use this API for text analysis purpose, it is too less. For example, when I wrote "new year resolution" (here), there are around 10K tweets in few days.
Now you have to use "next token" to solve this issue. Query parameter has a field to set "next token". If you don't set any in this field and get 100 tweets, it will give you the latest 100 data and you will find the data "next toke" at the end of the data set if you have more than 100 data. When you want to get next 100 data, you have to set this next page token in query parameter. You will find this next token every time, so if you repeat making requests with this token you will be able to reach to 10k tweets after 100 requests.

Ok. Now you will be able to get whatever data you want to get from twitter,,, I think. I'll go for the next step in the next article. Ummm this series always goes longer than I expect. Sorry for that.

Applive (2020), Twitter(ツイッター)検索コマンド, https://mag.app-liv.jp/archive/81735/#410863

この記事が気に入ったらサポートをしてみませんか?