Code Monkey home page Code Monkey logo

twitter-crawler's Introduction

Crawl Twitter Social Networks using the Twitter API v2 with Tweepy and Python.

Table of contents:


โญ Introduction

This repository contains a Twitter crawler that is able to crawl the following data using Twitter API v2 with the Tweepy library and Python:

  • User profile (e.g., name, profile description, following count, follower count, tweet count, etc.)
  • Tweet content (e.g., text, author, hashtag, topic, etc.)
  • Follow interactions
  • Post interactions
  • Retweet interactions
  • Like interactions

๐Ÿ”‘ Prerequisites

In order to work with Twitter API v2, you have to sign up for a developer account to get keys and tokens for Twitter API access. Learn more about how to access the Twitter API

  1. Sign up for a Twitter developer account.
  2. Create a Project and App.
  3. Find or generate the following credentials within your developer App:
    • Consumer Key and Secret
    • Access Token and Secret
    • Bearer Token

Basically, Twitter allows you to access tweets from the last 7 days only. If you would like to access the full archive of tweets, Academic Research access is required. Learn more about Acedemic Research access


๐Ÿ“– Dependencies

The script is implemented under the following dependencies:

  • tweepy==4.13.0
  • pandas==2.0.0
  • omegaconf==2.3.0
  • tqdm==4.65.0

Install all dependencies:

pip install -r requirements.txt

โš™๏ธ Configure the crawler

You can manage configuration at config.yaml.

  1. Specify whether you have Academic Research access.
ACADEMIC_ACCESS: True
  1. Place your credentials.
# Place your credentials.
CONSUMER_KEY: "<REPLACE ME>"
CONSUMER_SECRET: "<REPLACE ME>"
ACCESS_TOKEN: "<REPLACE ME>"
ACCESS_SECRET: "<REPLACE ME>"
BEARER_TOKEN: "<REPLACE ME>"
  1. Specify your base query, tweet field, user field, time period, and crawling limit.

๐Ÿ” Build queries

You can specify your queries in query.json. Learn more about how to build queries

  • Ex: Searching for tweets that contain the keyword "twitter" from "Elon Musk".
[
    {
        "context": "",
        "keyword": "twitter",
        "user": "from:elonmusk"
    }
]

You can also search by a specific domain and entity. Learn more about tweet annotations and See all available domain and entity

  • Ex: Searching for tweets within the "Tech News" domain that contain the hashtag "#tech" and one of "#apple" or "#iphone".
[
    {
        "context": "context:131.840160819388141570",
        "keyword": "#tech (#apple OR #iphone)",
        "user": ""
    }
]

โ–ถ๏ธ Run the crawler

You can run the crawler using the following command:

python crawler.py
You can also parse arguments to the command:
python crawler.py [--cfg] <config_file>

๐Ÿ‘€ View the crawler output

Once crawling is finished, the following .csv files are created under the ./data directory:

data
โ””โ”€ tweet.csv
โ””โ”€ user.csv
โ””โ”€ follow.csv
โ””โ”€ post.csv
โ””โ”€ retweet.csv
โ””โ”€ like.csv

Examples of crawler outputs:

  • Examples of tweet.csv:

    id author_id text
    1649919766742614017 44196397 'The least bad solution to the AGI control problem that I can think of is to give every verified human a vote'
  • Examples of user.csv:

    id name username description verified followers_count following_count tweet_count
    44196397 Elon Musk elonmusk nothing True 136395633 241 25051
  • Examples of follow.csv:

    user_id following_id
    44196397 797727562235609088
  • Examples of post.csv, retweet.csv, and like.csv:

    user_id tweet_id
    44196397 1649919766742614017

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.