我正在尝试编写一个执行以下操作的简单脚本:
我计划使用crontab来执行(1)。另外,这是我到目前为止为一个特定网站提出的脚本:
from selenium import webdriver
import smtplib
import sys
driver = webdriver.Firefox()
#Capital Pacific Website
#Commerical Real Estate
#open text file containing property titles we already know about
properties = open("properties.txt", "r+")
currentList = []
for line in properties:
currentList.append(line)
#to search for new listings
driver.get("http://cp.capitalpacific.com/Properties")
assert "Capital" in driver.title
#holds any new listings
newProperties = []
#find all listings on page by Property Name
newList = driver.find_elements_by_class_name('overview')
#find elements in pageList not in oldList & add to newList
#add new elements to
for x in currentList:
for y in newList:
if y != x:
newProperties.append(y)
properties.write(y)
properties.close()
driver.close()
#if no new properties found, terminate script
#else, email properties
if not newProperties:
sys.exit()
else:
fromaddr = 'someone@gmail.com'
toaddrs = ['someoneelse@yahoo.com']
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
for item in newProperties:
msg = item
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
到目前为止我遇到的问题:(请在这里忍受,因为我是一个蟒蛇新手......)
使用列表存储使用selenium返回的网络元素"按类"方法:是否有更好的方法从文本文件写入/输出以确保我只获取新添加的属性?
如果脚本确实找到了网站上但不在newList上的类属性,那么我是否可以通过该div来获取有关列表的详细信息?
请提出任何建议/建议!谢谢。
答案 0 :(得分:0)
如果您切换到使用列表存储为词典的JSON
格式,该怎么办?
[
{
"location": "REGON CITY, OR",
"price": 33000000,
"status": "active",
"marketing_package_url": "http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf"
...
},
...
]
为了识别新的物品,您需要有关每个物业的独特信息。例如,您可以使用营销包网址 - 对我来说看起来很独特。
以下是从页面获取商家信息列表的示例代码:
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')
properties.append({
'title': title.text,
'location': location.text,
'marketing_package_url': marketing_package.getAttribute('href')
})