Difference between revisions of "Scrapping: Google Search"
Onnowpurbo (talk | contribs) (Created page with "Berikut ini adalah '''script Python sederhana''' untuk scraping hasil pencarian Google berdasarkan '''keyword''', lalu '''menyimpan hasilnya ke file (CSV atau TXT)'''. '''Cat...") |
Onnowpurbo (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 6: | Line 6: | ||
− | pip install googlesearch-python | + | pip install googlesearch-python nltk matplotlib wordcloud |
+ | |||
Line 12: | Line 13: | ||
import csv | import csv | ||
− | def google_scrape(keyword, num_results= | + | def google_scrape(keyword, num_results=40, |
+ | output_file='results.csv'): | ||
print(f"Searching Google for: {keyword}") | print(f"Searching Google for: {keyword}") | ||
− | + | ||
− | # Lakukan pencarian | + | # Lakukan pencarian dan ubah hasil ke dalam list |
− | results = search(keyword, num_results=num_results) | + | results = list(search(keyword, num_results=num_results)) |
− | + | ||
# Simpan ke file CSV | # Simpan ke file CSV | ||
with open(output_file, mode='w', newline='', encoding='utf-8') as file: | with open(output_file, mode='w', newline='', encoding='utf-8') as file: | ||
Line 24: | Line 26: | ||
for url in results: | for url in results: | ||
writer.writerow([keyword, url]) | writer.writerow([keyword, url]) | ||
− | + | ||
− | print(f"Saved {len(results)} results to '{output_file}'") | + | print(f"Saved {len(results)} results to '{output_file}'") |
# Contoh penggunaan | # Contoh penggunaan | ||
keyword = "berita terbaru teknologi Indonesia" | keyword = "berita terbaru teknologi Indonesia" | ||
− | google_scrape(keyword, num_results= | + | google_scrape(keyword, num_results=40) |
==Output== | ==Output== |
Latest revision as of 12:46, 30 March 2025
Berikut ini adalah script Python sederhana untuk scraping hasil pencarian Google berdasarkan keyword, lalu menyimpan hasilnya ke file (CSV atau TXT).
Catatan: Kita tidak bisa scraping langsung dari `https://www.google.com` karena Google melindungi halamannya dengan anti-bot. Sebagai alternatif legal dan lebih stabil, kita bisa gunakan SerpApi (gratis hingga 100 permintaan/bulan) atau Bing Search sebagai fallback. Namun, untuk langsung scraping halaman Google, kita bisa pakai `googlesearch` dari `googlesearch-python`.
✅ Script dengan `googlesearch-python` (tanpa API)
pip install googlesearch-python nltk matplotlib wordcloud
from googlesearch import search import csv def google_scrape(keyword, num_results=40, output_file='results.csv'): print(f"Searching Google for: {keyword}") # Lakukan pencarian dan ubah hasil ke dalam list results = list(search(keyword, num_results=num_results)) # Simpan ke file CSV with open(output_file, mode='w', newline=, encoding='utf-8') as file: writer = csv.writer(file) writer.writerow(['Keyword', 'URL']) for url in results: writer.writerow([keyword, url]) print(f"Saved {len(results)} results to '{output_file}'") # Contoh penggunaan keyword = "berita terbaru teknologi Indonesia" google_scrape(keyword, num_results=40)
Output
File `results.csv` akan berisi:
Keyword,URL berita terbaru teknologi Indonesia,https://tekno.tempo.co/... berita terbaru teknologi Indonesia,https://inet.detik.com/...
Opsi Lain: Versi Tanpa `googlesearch`, Full Scraping (Lebih Riskan)
Kalau kamu ingin versi yang benar-benar scraping HTML dari Google, kamu bisa pakai `requests` dan `BeautifulSoup` tapi kemungkinan akan diblokir atau gagal karena Google deteksi bot. Rekomendasi: pakai proxy atau API seperti SerpApi jika ingin lebih stabil dan legal.