粗大メモ置き場

個人用,たまーに来訪者を意識する雑記メモ

Twintを使ってTweet情報を収集する(Python3)

前回の補足のような記事です。

概要

  • Python上で使えるTwintパッケージを用いてTweet情報を収集する
  • Twintを使って特定ユーザのTweetを収集する
  • TweetsScraperに比べてデータ取得範囲が広い(自分への返信も見れる)

github.com

関連記事

  • Tweetscraperを用いた場合

ossyaritoori.hatenablog.com

Python環境でのインストールと使い方

pipを用いてインストールして実行する場合

  • 環境(イメージ):python:3.7-slim-stretch
  • 5分足らずでインストール・実行可

インストールは以下のコマンドでできました。

pip3 install --user --upgrade git+https://github.com/twintproject/twint.git@origin/master#egg=twint

特定のusernameを検索するなら

twint -u username -o output.json

とすれば該当するusernameで検索できます。

コマンドは公式を見ましょう。

GitHub - twintproject/twint: An advanced Twitter scraping & OSINT tool written in Python that doesn't use Twitter's API, allowing you to scrape a user's followers, following, Tweets and more while evading most API limitations.

Dockerイメージをpullして実行

自前でDockerimageをビルドするかDocker Hubから引っ張ってくる事ができます。 どちらも試しましたが後者がおすすめです。

自分の場合は以下のlatestのimageを用いました。

github.com

使い方は簡単で以下のコマンドを実行すれば/dataというフォルダにoutput.jsonが生成されます。

docker pull x0rzkov/twint:latest
docker run -ti --rm -v $(pwd)/data:/opt/twint/data x0rzkov/twint:latest -u <USERNAME> -o output.json --json

より詳細な説明は以下を参照してください。

github.com

Pythonで出力ファイルを確認

Pythonで出力を確認します。

ファイルごとopenしてjson.load()した場合,様式についてエラーが出ます。

中身をよく見ると1行毎にjson形式で書かれていてリストの形になっていないためのようなので下記のように一行一行読んで行きます。

import json
data=[]
with open("output.json", 'r',encoding="utf-8") as f:
    for line in f:
        data.append(json.loads(line))

TweetsScraperとの比較

得られる情報はほとんど遜色がなく,linkurlsになっていたりtweet_idconversation_idになっていたりなど細かいものが多いです。 一番大きな変更点は,自分への返信Tweetも取得できている点です。

{'cashtags': [],
  'conversation_id': '1289395426723016708',
  'created_at': 1596250862000,
  'date': '2020-08-01',
  'geo': '',
  'hashtags': [],
  'id': 1289395689697337344,
  'likes_count': 3,
  'link': 'https://twitter.com/slam_hub/status/1289395689697337344',
  'mentions': [],
  'name': 'SLAM-Hub',
  'near': '',
  'photos': [],
  'place': '',
  'quote_url': '',
  'replies_count': 0,
  'reply_to': [{'user_id': '1244129482132209664', 'username': 'slam_hub'}],
  'retweet': False,
  'retweet_date': '',
  'retweet_id': '',
  'retweets_count': 2,
  'source': '',
  'time': '03:01:02',
  'timezone': 'UTC',
  'trans_dest': '',
  'trans_src': '',
  'translate': '',
  'tweet': 'PointContrast: Unsupervised Pre-training for 3D Point Cloud Understanding (ECCV2020)\nPaper:  https://arxiv.org/abs/2007.10985\xa0',
  'urls': ['https://arxiv.org/abs/2007.10985'],
  'user_id': 1244129482132209664,
  'user_rt': '',
  'user_rt_id': '',
  'username': 'slam_hub',
  'video': 0}

これにより,会話をトラッキングできたり,conversation_idにより補足情報を関連付けることができます。

例えば,

# conversation_idを記録
ids = []
for dic in data:
    ids.append(dic['conversation_id'])

# idの出現回数を数える
from collections import Counter
Counter(ids)

とすれば,会話毎に何Tweetあるか確認できます。

Counter({'1247356052657455105': 1,
         '1248070308080177152': 2,
         '1248436573865041920': 2,
         '1248796223961624576': 2,
         '1249155400122994688': 1,
         '1249517662847283201': 2,…(以下略)