我希望脚本通过电子邮件发送newProperties列表中的任何对象。列表中的每个对象都是这样的:
newProperties[0] = {"marketing_package_url": "http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf", "location": "OREGON CITY, OR", "title": "TRAILS END MARKETPLACE", "contact": "mailto:mhorwitz@capitalpacific.com"} \
newProperties[1] = {"marketing_package_url": "http://cp.capitalpacific.com/Properties/Yukon-Village-YukonOK.pdf", "location": "YUKON, OK", "title": "YUKON VILLAGE", "contact": "mailto:rsanner@capitalpacific.com"}
...
/完整脚本
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("properties.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
newProperties = []
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()
#find the properties that we already have by cross referencing the
#marketing URLS
for x in oldProperties:
for item in newProperties:
if (item['marketing_package_url'] == x):
newProperties.remove(item)
#properties now has only the new properties
#add them to the file
with open('properties.txt', 'w') as outfile:
for item in newProperties:
json.dump(item, outfile)
#if no new properties found, terminate script
#else, email properties
#if no new properties found, terminate script
#else, email properties
if len(newProperties) == 0:
sys.exit()
else:
fromaddr = 'osxxxxxt@gmail.com'
toaddrs = ['osfsfsar@onid.oregonstate.edu']
username = 'xxxx@gmail.com'
password = '0fdrfdf'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
for item in newProperties:
msg = str(item) #PROBLEM HERE
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
问题是当前发送的电子邮件是空的。我使用str(item)来发送页面上的任何新列表,但似乎有问题将dict变成字符串然后发送它。有谁知道我怎么解决这个问题?