bocchilorenzo / ntscraper

Scrape from Twitter using Nitter instances
MIT License
176 stars 29 forks source link
nitter python scraper twitter

Unofficial Nitter scraper

Note

Twitter has recently made some changes which affected every third party Twitter client, including Nitter. As a result, most Nitter instances have shut down or will shut down shortly. Even local instances are affected by this, so you may not be able to scrape as many tweets as expected, if at all.

The scraper

This is a simple library to scrape Nitter instances for tweets. It can:

If the instance to use is not provided to the scraper, it will use a random public instance. If you can, please host your own instance in order to avoid overloading the public ones and letting Nitter stay alive for everyone. You can read more about that here: https://github.com/zedeus/nitter#installation.


Installation

pip install ntscraper

How to use

First, initialize the library:

from ntscraper import Nitter

scraper = Nitter(log_level=1, skip_instance_check=False)

The valid logging levels are:

The skip_instance_check parameter is used to skip the check of the Nitter instances altogether during the execution of the script. If you use your own instance or trust the instance you are relying on, then you can skip set it to 'True', otherwise it's better to leave it to false.

Then, choose the proper function for what you want to do from the following.

Scrape tweets

github_hash_tweets = scraper.get_tweets("github", mode='hashtag')

bezos_tweets = scraper.get_tweets("JeffBezos", mode='user')

Parameters:

Returns a dictionary with tweets and threads for the term.

Multiprocessing

You can also scrape multiple terms at once using multiprocessing:

terms = ["github", "bezos", "musk"]

results = scraper.get_tweets(terms, mode='term')

Each term will be scraped in a different process. The result will be a list of dictionaries, one for each term.

The multiprocessing code needs to run in a if __name__ == "__main__" block to avoid errors. With multiprocessing, only full logging is supported. Also, the number of processes is limited to the number of available cores on your machine.

NOTE: using multiprocessing on public instances is highly discouraged since it puts too much load on the servers and could potentially also get you rate limited. Please only use it on your local instance.

Get single tweet

tweet = scraper.get_tweet_by_id("x", "1826317783430303888")

Parameters:

Returns a dictionary with the tweet's content.

Get profile information

bezos_information = scraper.get_profile_info("JeffBezos")

Parameters:

Returns a dictionary of the profile's information.

Multiprocessing

As for the term scraping, you can also get info from multiple profiles at once using multiprocessing:

usernames = ["x", "github"]

results = scraper.get_profile_info(usernames)

Each user will be scraped in a different process. The result will be a list of dictionaries, one for each user.

The multiprocessing code needs to run in a if __name__ == "__main__" block to avoid errors. With multiprocessing, only full logging is supported. Also, the number of processes is limited to the number of available cores on your machine.

NOTE: using multiprocessing on public instances is highly discouraged since it puts too much load on the servers and could potentially also get you rate limited. Please only use it on your local instance.

Get random Nitter instance

random_instance = scraper.get_random_instance()

Returns a random Nitter instance.

Note

Due to recent changes on Twitter's side, some Nitter instances may not work properly even if they are marked as "working" on Nitter's wiki. If you have trouble scraping with a certain instance, try changing it and check if the problem persists.

To do list