Blog Untuk Semua

Everything for Everyone

Collect Email from Google using Python

1 Komentar

Mengumpulkan email dari google menggunakan python, ide ini lumayan brilliant kalau kita lagi membutuhkan email buat promosi barang ataupun produk tertentu yang ingin kita jual. Jadi tidak perlu lagi keluar-masuk website orang buat dump email dari database-nya. Langsung gunakan search engine raksasa buat mengumpulkannya yaitu “Google”. Berikut potongan kode program yang saya buat, mungkin masih banyak kekurangannya tapi buat awal-awal lumayan lah🙂.

Modul yang dibutuhkan :

#!/usr/bin/env python

import urllib2
import time
import re
import MySQLdb
from google import search

def write_db(result):
 conn = MySQLdb.connect(host="localhost", user="root", passwd="", db="email_db")
 cursor = conn.cursor()
 insert = "INSERT INTO email_tbl(mail_address) VALUES(\""+result+"\")"
 select = "SELECT * FROM email_tbl"
 cursor.execute(insert)
 count = cursor.execute(select)
 print "1 EMAIL HAS BEEN ADDED -> ",result,"\t\t\t\t total email :",count
 cursor.close()
 conn.commit()

def select_db(raw_email):
 conn = MySQLdb.connect(host="localhost", user="root", passwd="", db="email_db")
 cursor = conn.cursor() 
 select = "SELECT mail_address from email_tbl WHERE mail_address=\""+raw_email+"\""
 cursor.execute(select)
 res = cursor.execute(select) # jika email di database ada yang ganda, res bernilai lebih dari 0
 if res > 0:
  return 1
 cursor.close()
 conn.close()

def get_email(link):
 #####optional regex for email
 #pattern = \
 #re.compile(r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
 ###########
 pattern = re.compile(r"\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b")
 get_link = urllib2.urlopen(link)
 content = get_link.read()
 email = pattern.findall(content)
 for x in email:
  if select_db(x)==1:
   #print '\temail duplicate -> '+x
   continue
  time.sleep(0.5)
  write_db(x)

def get_url(key):
 for url in search(key, stop=0):
  time.sleep(1)
  get_email(url)

if __name__ == "__main__":
 try:
  keywords = raw_input("input keyword : ")
  get_url(keywords)
 except KeyboardInterrupt:
  print '\n\nprogram exit !!'

Lalu buat database nya :
nama database : email_db

nama tabel : email_tbl

atribut tabel : no_id, mail_address (no_id di set sebagai primay key dan auto increment)

Program ini sudah berhasil saya jalankan di OS Windows 7. Semoga bermanfaat🙂.

One thought on “Collect Email from Google using Python

  1. Now, the translation may be wrong, but…

    “Collecting email from google using python, this is pretty brilliant idea if we still need a promotional item or email for a specific product we want to sell.”

    You mean spam? Seriously?😛

    “So do not need to out-do the website in an email from a database dump her.”

    Wow. Let’s hope the translator mangled this one, because if it says you hack websites and dump the database for a leaving, your life sucks, man.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s