Skip to content

Instantly share code, notes, and snippets.

@hbro
Last active June 8, 2021 17:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hbro/e8a640851bb8b076c37394903f28adf4 to your computer and use it in GitHub Desktop.
Save hbro/e8a640851bb8b076c37394903f28adf4 to your computer and use it in GitHub Desktop.
Scrape an Immoweb search URL and e-mail any new results.
#!/usr/bin/python3
# Usage:
# - Fill in the settings, then run with `python3 ImmowebScraper.py`.
# - First run won't send any mails (or you'd get dozens at once).
# Requirements:
# - python3
# - selenium
# - phantomjs
import sqlite3
from selenium import webdriver
import smtplib
from email.mime.text import MIMEText
# settings
url = 'http://www.immoweb.be/nl/zoek/huis/te-koop?zips=2300,2440,2460&minprice=200000&maxprice=350000&minroom=3&maxroom=4'
maxpages = 5
emailaddress = 'notarealemail@gmail.com'
smtp_host = 'smtp.gmail.com'
smtp_port = '587'
smtp_mail = 'notarealemail@gmail.com'
smtp_user = 'notarealemail@gmail.com'
smtp_pass = 'ohnoyoudont'
# prep
db = sqlite3.connect('ImmowebScraper.db')
c = db.cursor()
browser = webdriver.PhantomJS('/usr/local/bin/phantomjs')
browser.implicitly_wait(5)
smtpserver = smtplib.SMTP(smtp_host, smtp_port)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.login(smtp_user, smtp_pass)
# create the immos table
c.execute('CREATE TABLE IF NOT EXISTS immos (id INTEGER PRIMARY KEY UNIQUE NOT NULL);')
db.commit()
# if there are no id's yet, this is the first run
c.execute('SELECT COUNT(*) FROM immos;')
firstRun = c.fetchone()[0] == 0
# zhu li, do the thing
for page in range(1,maxpages+1):
print('Browsing page {} ...'.format(page))
browser.get(url + '&page=' + str(page))
results = browser.find_elements_by_xpath('//div[@id="result"]/div')
for result in results:
immoweb_id = result.get_attribute('id')
c.execute('SELECT COUNT(*) FROM immos WHERE id=:id;', {'id':immoweb_id})
if c.fetchone()[0] == 0:
print('New property found: ID {}! Storing in db.'.format(immoweb_id))
c.execute('INSERT INTO immos(id) VALUES (:id);', {'id':immoweb_id})
db.commit()
if not firstRun:
print('Sending mail about new property ID {}.'.format(immoweb_id))
immoweb_text = result.find_element_by_tag_name('a').text
immoweb_url = result.find_element_by_tag_name('a').get_attribute('href')
email = MIMEText(immoweb_text + '\nURL: ' + immoweb_url)
email['Subject'] = 'New property on Immoweb: {}'.format(immoweb_id)
email['From'] = smtp_user
email['To'] = emailaddress
smtpserver.sendmail(smtp_user,emailaddress,email.as_string())
smtpserver.quit()
db.close()
@enjofaes
Copy link

How does it work exactly? I'm new to this. Do I change this?
emailaddress = 'notarealemail@gmail.com'
smtp_host = 'smtp.gmail.com'
smtp_port = '587'
smtp_mail = 'notarealemail@gmail.com'
smtp_user = 'notarealemail@gmail.com'
smtp_pass = 'ohnoyoudont'

@hbro
Copy link
Author

hbro commented Jan 26, 2021

How does it work exactly? I'm new to this. Do I change this?

Yes, simply replace the values between quotes with your connections details. This is different for every provider though. There is a real probability that this script does not work anymore though, as Immoweb likely changed their looks and feels in the mean time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment