Powerful Twitter/X scraping tool dengan Selenium
Project description
🐦 Panen Tweet - Twitter/X Scraper
Panen Tweet adalah tool powerful untuk scraping Twitter/X menggunakan Selenium. Dengan tool ini, Anda dapat mengekstrak tweet berdasarkan kata kunci, rentang tanggal, bahasa, dan jenis tweet (teratas/terbaru) dengan mudah.
📦 Instalasi
Instalasi dari PyPI (Recommended)
pip install panen-tweet
Instalasi dari Source
git clone https://github.com/Dhaniaaa/panen-tweet.git
cd panen-tweet
pip install -e .
🐧 Running on Google Colab / Linux Server
Jika Anda menjalankan di Google Colab atau Linux Server (VPS), Anda WAJIB menginstall Google Chrome.
Kami sudah menyertakan perintah otomatis untuk ini:
# 1. Install library
!pip install panen-tweet
# 2. Install Google Chrome (Sekali jalan)
!panen-tweet install-chrome
🔑 Mendapatkan Auth Token
Sebelum menggunakan, Anda perlu mendapatkan auth_token dari akun Twitter/X Anda:
Langkah-langkah:
- Login ke x.com menggunakan browser Anda
- Tekan F12 untuk membuka Developer Tools
- Buka tab Application (Chrome) atau Storage (Firefox)
- Di sidebar kiri, expand Cookies → klik
https://x.com - Cari cookie dengan nama
auth_token - Klik pada cookie tersebut dan salin nilai (value) nya
- Nilai ini yang akan Anda gunakan
⚠️ PENTING - Keamanan Token:
- JANGAN bagikan auth_token Anda kepada siapapun
- JANGAN commit auth_token ke Git/GitHub
- Token ini memberikan akses penuh ke akun Twitter/X Anda
- Jika token ter-expose, segera ganti password Twitter/X Anda
🚀 Cara Penggunaan
Opsi 1: Command Line Interface (Termudah)
Setelah instalasi, jalankan command berikut di terminal:
panen-tweet
Program akan meminta Anda untuk:
- Memasukkan auth_token
- Kata kunci yang ingin dicari
- Jumlah tweet maksimal per sesi
- Tanggal mulai (format: YYYY-MM-DD)
- Tanggal selesai (format: YYYY-MM-DD)
- Interval hari per sesi scraping
- Kode bahasa (contoh:
iduntuk Indonesia,enuntuk English) - Jenis tweet (1 untuk Top, 2 untuk Terbaru/Latest)
Contoh interaksi:
TWITTER/X SCRAPER - PANEN TWEET
...
Masukkan auth_token Anda: <paste_token_anda_disini>
1. Masukkan kata kunci/topik pencarian: python programming
2. Berapa jumlah MAKSIMAL tweet yang ingin diambil PER SESI? 100
3. Masukkan TANGGAL MULAI KESELURUHAN (YYYY-MM-DD): 2024-01-01
4. Masukkan TANGGAL SELESAI KESELURUHAN (YYYY-MM-DD): 2024-01-07
5. Berapa hari interval per sesi scraping? (misal: 1 untuk per hari): 1
6. Masukkan kode bahasa (misal: 'id' untuk Indonesia, 'en' untuk Inggris): en
7. Pilih jenis tweet (1 untuk Top, 2 untuk Terbaru): 2
Program akan otomatis:
- Scraping tweet sesuai parameter
- Menyimpan hasil ke file CSV
- Menghapus duplikat otomatis
Output: File CSV dengan nama seperti tweets_pythonprogramming_latest_20240101-20240107.csv
Opsi 2: Menggunakan sebagai Library Python
Jika Anda ingin mengintegrasikan ke dalam kode Python Anda:
from panen_tweet import TwitterScraper
import datetime
import os
# Setup auth token (gunakan environment variable untuk keamanan)
# Windows: $env:TWITTER_AUTH_TOKEN = "your_token_here"
# Linux/Mac: export TWITTER_AUTH_TOKEN="your_token_here"
auth_token = os.getenv('TWITTER_AUTH_TOKEN', 'your_auth_token_here')
# Inisialisasi scraper
scraper = TwitterScraper(
auth_token=auth_token,
scroll_pause_time=5, # Waktu jeda antar scroll (detik)
headless=True # Set False untuk melihat browser
)
# Scraping dengan rentang tanggal
df = scraper.scrape_with_date_range(
keyword="python programming",
target_per_session=100, # 100 tweets per sesi
start_date=datetime.datetime(2024, 1, 1), # Format: YYYY-MM-DD
end_date=datetime.datetime(2024, 1, 7), # Format: YYYY-MM-DD
interval_days=1, # Scraping per hari
lang='en', # Bahasa English
search_type='latest' # 'top' atau 'latest'
)
# Simpan hasil ke CSV
if df is not None:
scraper.save_to_csv(df, "hasil_scraping.csv")
print(f"✅ Berhasil mengambil {len(df)} tweets!")
print(df.head())
else:
print("❌ Tidak ada data yang berhasil diambil")
Opsi 3: Menggunakan Environment Variable (Recommended untuk Security)
Windows PowerShell:
# Set environment variable
$env:TWITTER_AUTH_TOKEN = "your_auth_token_here"
# Jalankan program
panen-tweet
Windows CMD:
set TWITTER_AUTH_TOKEN=your_auth_token_here
panen-tweet
Linux/Mac:
export TWITTER_AUTH_TOKEN="your_auth_token_here"
panen-tweet
Atau buat file .env:
# Install python-dotenv
pip install python-dotenv
# Buat file .env
echo "TWITTER_AUTH_TOKEN=your_token_here" > .env
# Di kode Python Anda:
from dotenv import load_dotenv
load_dotenv()
📊 Format Output
Data yang dihasilkan dalam format CSV dengan kolom-kolom berikut:
| Kolom | Deskripsi |
|---|---|
username |
Nama user yang memposting tweet |
handle |
Handle Twitter (@username) |
timestamp |
Waktu tweet diposting (format ISO) |
tweet_text |
Isi konten tweet |
url |
URL lengkap ke tweet |
reply_count |
Jumlah replies |
retweet_count |
Jumlah retweets |
like_count |
Jumlah likes |
Contoh output CSV:
username,handle,timestamp,tweet_text,url,reply_count,retweet_count,like_count
John Doe,@johndoe,2024-01-01T10:30:00.000Z,Python is awesome!,https://x.com/johndoe/status/...,5,10,25
...
⚙️ Parameter & Konfigurasi
TwitterScraper Parameters
TwitterScraper(
auth_token=None, # Cookie auth_token (WAJIB)
scroll_pause_time=5, # Waktu jeda antar scroll dalam detik
headless=True # True = tanpa GUI, False = tampilkan browser
)
scrape_with_date_range Parameters
scraper.scrape_with_date_range( keyword="", # Kata kunci pencarian (WAJIB) target_per_session=100, # Jumlah target tweet per sesi start_date=datetime, # Tanggal mulai (WAJIB) YYYY-MM-DD end_date=datetime, # Tanggal selesai (WAJIB) YYYY-MM-DD interval_days=1, # Interval hari per sesi (1 = per hari) lang='id', # Kode bahasa: 'id', 'en', 'ja', dll search_type='top' # 'top' = tweet teratas, 'latest' = terbaru )
## 💡 Tips & Tricks
### 1. Scraping Banyak Tweet
Untuk hasil maksimal:
- Gunakan interval kecil (1 hari) untuk dataset besar
- Set `target_per_session` tidak terlalu tinggi (50-200)
- Gunakan `scroll_pause_time` lebih besar (7-10 detik) untuk koneksi lambat
### 2. Menghindari Rate Limit
- Jangan scraping terlalu cepat
- Gunakan `scroll_pause_time` minimal 5 detik
- Beri jeda antar sesi scraping
- Jangan jalankan multiple instance bersamaan
### 3. Filter Bahasa Spesifik
Gunakan parameter `lang` dengan kode ISO 639-1:
- `id` - Bahasa Indonesia
- `en` - English
- `ja` - Japanese
- `es` - Spanish
- `fr` - French
- Dan lainnya...
### 4. Troubleshooting "No tweets found"
Jika tidak menemukan tweet:
- Periksa koneksi internet
- Pastikan auth_token masih valid
- Coba dengan keyword yang lebih umum
- Periksa rentang tanggal (mungkin tidak ada tweet di periode tersebut)
## 📋 Requirements
- **Python 3.7+**
- **Chrome/Chromium browser**
- **Dependencies** (otomatis terinstall):
- pandas >= 2.0.0
- selenium >= 4.0.0
- webdriver-manager >= 4.0.0
## ⚠️ Disclaimer & Legal
Tool ini dibuat untuk tujuan **edukasi dan penelitian**.
Pastikan Anda mematuhi:
- [Twitter/X Terms of Service](https://twitter.com/tos)
- [Twitter/X Developer Agreement](https://developer.twitter.com/en/developer-terms/agreement-and-policy)
- Kebijakan scraping dan rate limiting
- Privasi dan hak cipta pengguna lain
**Penulis tidak bertanggung jawab** atas penyalahgunaan tool ini.
## 🐛 Troubleshooting
### Error: "WebDriver not found"
**Solusi:** Package otomatis download ChromeDriver, pastikan Chrome terinstall.
### Error: "Auth token invalid"
**Solusi:**
1. Login ulang ke x.com
2. Dapatkan auth_token baru
3. Pastikan tidak ada spasi saat copy-paste
### Error: "No tweets found"
**Solusi:**
- Periksa koneksi internet
- Verifikasi auth_token masih valid
- Coba keyword lain atau rentang tanggal berbeda
### Browser tidak muncul (headless mode)
Ini normal! Set `headless=False` jika ingin melihat browser:
```python
scraper = TwitterScraper(auth_token=token, headless=False)
🤝 Contributing
Kontribusi sangat diterima!
- Fork repository ini
- Buat branch baru (
git checkout -b feature/AmazingFeature) - Commit perubahan (
git commit -m 'Add some AmazingFeature') - Push ke branch (
git push origin feature/AmazingFeature) - Buat Pull Request
📝 License
MIT License - lihat file LICENSE untuk detail lengkap.
💬 Support & Contact
- GitHub Issues: https://github.com/Dhaniaaa/panen-tweet/issues
- PyPI Package: https://pypi.org/project/panen-tweet/
- Email: rhamadhanigb19@gmail.com
🙏 Acknowledgments
- Selenium - Web automation framework
- webdriver-manager - Automatic ChromeDriver management
- pandas - Data processing
Made with ❤️ for the data science & research community
⭐ Jika project ini membantu, berikan star di GitHub!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file panen_tweet-1.0.5.tar.gz.
File metadata
- Download URL: panen_tweet-1.0.5.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f891b28cc0afbd4654498a5b590f6b692f1823f31d847c8cfe54a3cfb309b38
|
|
| MD5 |
0a67f04f3434fa3333fc796c0afa9f09
|
|
| BLAKE2b-256 |
6efa0252575b2fb78c4610f910654e8d8475c7a33035518f9c3ef5e3d55e12a5
|
Provenance
The following attestation bundles were made for panen_tweet-1.0.5.tar.gz:
Publisher:
workflows.yaml on DhaniAAA/panen-tweet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
panen_tweet-1.0.5.tar.gz -
Subject digest:
6f891b28cc0afbd4654498a5b590f6b692f1823f31d847c8cfe54a3cfb309b38 - Sigstore transparency entry: 861863926
- Sigstore integration time:
-
Permalink:
DhaniAAA/panen-tweet@3f282e07527cbbca3d8308164ff5925878a53dfc -
Branch / Tag:
refs/tags/v1.0.6 - Owner: https://github.com/DhaniAAA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflows.yaml@3f282e07527cbbca3d8308164ff5925878a53dfc -
Trigger Event:
release
-
Statement type:
File details
Details for the file panen_tweet-1.0.5-py3-none-any.whl.
File metadata
- Download URL: panen_tweet-1.0.5-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb7c8605c047c440b9c70622796f879500f9360d7a8d22c69f513913f2f99851
|
|
| MD5 |
7e6aa50743ef58f01539b7f2347979ab
|
|
| BLAKE2b-256 |
f8076e2c9e23a664e3c1b73a989b2ea8869b9e4f3f46030c11fdc6ec22525ddd
|
Provenance
The following attestation bundles were made for panen_tweet-1.0.5-py3-none-any.whl:
Publisher:
workflows.yaml on DhaniAAA/panen-tweet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
panen_tweet-1.0.5-py3-none-any.whl -
Subject digest:
eb7c8605c047c440b9c70622796f879500f9360d7a8d22c69f513913f2f99851 - Sigstore transparency entry: 861863962
- Sigstore integration time:
-
Permalink:
DhaniAAA/panen-tweet@3f282e07527cbbca3d8308164ff5925878a53dfc -
Branch / Tag:
refs/tags/v1.0.6 - Owner: https://github.com/DhaniAAA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflows.yaml@3f282e07527cbbca3d8308164ff5925878a53dfc -
Trigger Event:
release
-
Statement type: