Skip to content

About

A Python script to fetch messages from Telegram channels. Filters the messages based on keywords, cleans the text, and saves the results to a JSON file. Useful for archiving posts, data analysis, or content monitoring.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

Telegram Channel Scraper

A Python script to fetch messages from Telegram channels. Filters the messages based on keywords, cleans the text, and saves the results to a JSON file. Useful for archiving posts, data analysis, or content monitoring.


Features

  • Connects to Telegram using Telethon.
  • Fetches the latest messages from a specific channel.
  • Filters out messages containing banned keywords.
  • Removes unwanted tags from messages.
  • Avoids saving duplicate messages.
  • Saves messages to a JSON file with metadata (id, date, text, link).

Requirements

Install dependencies via pip:

pip install telethon python-dotenv

Setup

  1. Clone the repository:
git clone https://github.com/your-username/telegram-channel-scraper.git
cd telegram-channel-scraper
  1. Create a .env file in the project root with the following variables:
API_ID=your_telegram_api_id
API_HASH=your_telegram_api_hash
PHONE_NUMBER=your_phone_number_with_country_code
CHANNEL_USERNAME=@your_channel_username
FETCH_LIMIT=20               # optional, defaults to 20
BANNED_KEYWORDS=spam,ad,etc  # optional, comma-separated
TAGS_TO_REMOVE=#tag1,#tag2  # optional, comma-separated

Note: You can obtain API_ID and API_HASH by creating an application on my.telegram.org.


Usage

Run the script:

python telegram_scraper.py
  • The script will log in to Telegram using your phone number.
  • It fetches messages from the specified channel, filters them, and saves them in telegram_posts.json.
  • Skips duplicates if messages are already saved.

Output

The script saves messages in JSON:

[
  {
    "id": 123,
    "date": "2026-01-27 12:00:00",
    "text": "Cleaned message content",
    "link": "https://t.me/channel_username/123"
  }
]

Notes

  • Make sure your account has access to the channel you want to scrape.
  • BANNED_KEYWORDS and TAGS_TO_REMOVE are optional.
  • Always respect Telegram’s terms of service when scraping content.

License

This project is licensed under the MIT License.

About

A Python script to fetch messages from Telegram channels. Filters the messages based on keywords, cleans the text, and saves the results to a JSON file. Useful for archiving posts, data analysis, or content monitoring.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages