我编写了一个小脚本,用于检查本地网站的房地产列表,并根据存储在数据库中的列表检查新列表。然后通过电子邮件发送任何新的列表。
import pymongo
from selenium import webdriver
import smtplib
import sys
import json
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.properties
collection = db['capitalpacific']
#hold old properties imported from DB
oldProperties = []
#holds new properties gathered from page
newProperties = []
driver = webdriver.Firefox()
driver.get("http://cp.capitalpacific.com/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')
newProperties.append({
'title': title.text,
'location': location.text,
'marketing_package_url': marketing_package.get_attribute("href"),
'contact': contact_email.get_attribute("href")
})
driver.close()
'''if database not empty, add the old properties,
then compare against the newly fetched and remove repeats'''
if collection.count() != 0:
for post in collection.find():
oldProperties.append(post)
for oldListing in oldProperties:
for newListing in newProperties:
if oldListing['marketing_package_url'] == newListing['marketing_package_url']:
newProperties.remove(newListing)
'''if no new listings, exit the program. Otherwise, email all new
listings and then insert them into the DB'''
if len(newProperties) == 0:
sys.exit()
else:
fromaddr = 'myEmail@gmail.com'
toaddrs = ['myOtherEmail@gmail.com']
username = 'myEmail@gmail.com'
password = 'xxxxxxxxx'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
for item in newProperties:
fullMessage = "New Listing @ Capital Pacific: " + "\n"
fullMessage += "Title: " + str(item['title']) + "\n"
fullMessage += "Location: " + str(item['location']) + "\n"
fullMessage += "Contact Email: " + str(item['contact']) + "\n"
server.sendmail(fromaddr, toaddrs, fullMessage)
collection.insert(item)
server.quit()
这就是事情:它运作良好,并将任何新电子邮件发送到我的电子邮件就好了。但是,当我将我的辅助电子邮件地址(上面的例子中的myOtherEmail@gmail.com)替换为我的堂兄(生活在不同的状态)时,他声称获取空消息而不是包含数据的消息(这是我得到的)
可能导致这种情况的原因是什么?请大家好,这是我在Python上开始的第一批项目之一。为了澄清,他正在接收电子邮件,只是空白的(我获得了我的辅助(gmail)帐户的完整数据,他还有一个gmail帐户)。