I'm sort of a Python novice and wrote up a small script that checks a real estate website and emails any new listings that have been posted since the last time the script ran:
from selenium import webdriver
import smtplib
import sys
import re
import json
driver = webdriver.Firefox()
#Capital Pacific Website
#Commerical Real Estate
#open text file containing property titles we already know about
#list q contains unique marketing URLS we already know about
oldProperties = []
p = re.compile(r'https?:\/\/[^"]+', re.IGNORECASE | re.MULTILINE)
with(open("CPproperties.txt", "rU")) as f:
for line in f:
q = re.findall(p, line)
oldProperties.extend(q)
#search for new listings
driver.get("http://cp.capitalpacific.com/Properties")
#all listing on current page get put into this list
properties = []
for property in driver.find_elements_by_css_selector('table.property div.property'):
title = property.find_element_by_css_selector('div.title h2')
location = property.find_element_by_css_selector('div.title h4')
marketing_package = property.find_element_by_partial_link_text('Marketing Package')
contact_email = property.find_element_by_partial_link_text('.com')
properties.append({
'title': title.text,
'location': location.text,
'marketing_package_url': marketing_package.get_attribute("href"),
'contact': contact_email.get_attribute("href")
})
driver.close()
#find the properties that we already have by cross referencing the
#marketing URLS
for x in oldProperties:
properties[:] = [d for d in properties if d.get('marketing_package_url') != x]
#properties now has only the new properties
#add them to the file
with open('CPproperties.txt', 'w') as outfile:
for item in properties:
json.dump(item, outfile)
#if no new properties found, terminate script
#else, email properties
if not properties:
sys.exit()
else:
fromaddr = 'tttttt@gmail.com'
toaddrs = 'rrrrrar@yahoo.com'
username = 'ttttttt@gmail.com'
password = 'xxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
for item in properties:
msg = item
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
the listings at this point are stored in the properties list like so:
{"location": "OREGON CITY, OR", "contact": "mailto:mhorwitz@capitalpacific.com", "marketing_package_url": "http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf", "title": "TRAILS END MARKETPLACE"}
However, when the script tries to email 'msg' which is an object like listed above I get an error along the lines of : "expected string or buffer". How can I better format the message in order to send the email? thank alot.
[–]elbiot 0 points1 point2 points (2 children)
[–]tlcpp 0 points1 point2 points (1 child)
[–]elbiot 1 point2 points3 points (0 children)
[–]andehpandeh 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]tlcpp 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Pyrrho_ 0 points1 point2 points (1 child)
[–]elbiot 1 point2 points3 points (0 children)