【问题标题】:Checking Websites for Updates (Web Automation with Python + Selenium)检查网站是否有更新(使用 Python + Selenium 实现 Web 自动化)
【发布时间】:2015-03-06 11:29:09
【问题描述】:

我正在尝试编写一个简单的脚本来执行以下操作:

  1. 每 6 小时自动运行一次
  2. 检查房地产网站 新房源
  3. 如果找到新的列表详细信息,请通过电子邮件发送,否则 终止脚本直到下次运行

我打算使用 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()

到目前为止我的问题:(请耐心等待,因为我是 python 新手..)

使用列表存储使用 selenium 的“按类查找”方法返回的 Web 元素:是否有更好的方法从文本文件中写入/写入以确保我只获取新添加的属性?

如果脚本确实找到了网站上存在但 newList 上没有的类属性,有没有办法可以只通过该 div 以获取有关列表的详细信息?

请有任何建议/建议!谢谢。

【问题讨论】:

    标签: python selenium selenium-webdriver webautomation


    【解决方案1】:

    如果您改用JSON 格式并将列表存储为字典会怎样:

    [
        {
            "location": "REGON CITY, OR",
            "price": 33000000,
            "status": "active",
            "marketing_package_url": "http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf"
            ...
        },
        ...
    ]
    

    为了识别新的房源,您需要为每个房产提供独特的信息。例如,您可以为其使用营销包 url - 对我来说看起来很独特。

    这是一个从页面获取列表列表的示例代码:

    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')
        })
    

    【讨论】:

    • 感谢您的回复。我承认 JSON 可能是要走的路,但我不太熟悉它。您是否建议将 json 文件导入脚本到列表中?那么如何访问列表中的营销包 url 以比较新列表?
    • @RobertOttolia 我认为如果您使用 JSON,json 模块将有助于加载和转储到文件中。虽然我真的会考虑拥有一个普通的真实数据库来保存列表......
    • @RobertOttolia 谈论营销包 url - 添加到代码 sn-p 中,检查一下。
    • 非常感谢。但是,当我尝试运行 sn-p 时,我得到以下信息: title, location = property.find_elements_by_css_selector('div title h4') ValueError: need more than 1 value to unpack -- 这是否意味着我需要选择 h2 以及h4?
    • @RobertOttolia 我的错,更新代码,应该对你有用。谢谢!
    猜你喜欢
    • 2021-09-02
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2011-03-23
    相关资源
    最近更新 更多