在url中循环

2024-04-26 20:59:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个脚本,可以进入一个房地产拍卖网站,监控价格并提醒我任何变化。我最初一次监视一个url。我现在需要它来监测超过1个网址,并做同样的确切过程在同一时间。你知道吗

我尝试过在browser.get中循环url,但这似乎对我不起作用(idk如果我写错了,可能会)。但我需要它要么,打开一个新的标签tun并转到url,要么用另一种方式让它监视一切。你知道吗

这是我所有的密码

import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import ssl
from twilio.rest import Client
from twilio.rest import TwilioRestClient
from bs4 import BeautifulSoup as soup

urls = ['https://www.hubzu.com/property/90089436968-730-Trillium-Ln-Lilburn-GA-30047',
'https://www.hubzu.com/property/90016097522-225-Marriott-Ave-Schenectady-NY-12304',
'https://www.hubzu.com/property/90016098285-17-Spring-Meadows-Dr-Ormond-Beach-FL-32174'
]

browser = webdriver.Chrome()

# Start URL switch tryouts.

while True:
    for url in urls:
        browser.get((url))


       #This is the process i need all urls to do.
        time.sleep(2)
        address = soup(browser.page_source, 'html.parser').find('span', {'class':'h1'}).text
        propertyprice = browser.find_element_by_css_selector('span.current-bid')
        currentBidText = propertyprice.text
        try:                                
            WebDriverWait(browser, 90000).until_not(
                EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'span.current-bid'), currentBidText)
                )
        finally:
            print("+++ Send notifications.")
            account_sid = "***"
            auth_token = "***"
            client = Client(account_sid, auth_token)

            PhoneNumber1 = "+***"
            PhoneNumber2 = "+***"
            print("+ Send notifications to: ", PhoneNumber1, " and ", PhoneNumber2)

            sendTo1 = "{\"binding_type\":\"sms\",\"address\":\"" + PhoneNumber1 + "\"}"
            print("+ sendTo1: ", sendTo1)
            sendTo2 = "{\"binding_type\":\"sms\",\"address\":\"" + PhoneNumber2 + "\"}"
            print("+ sendTo2: ", sendTo2)

            notify_service_sid = "***"
            notification = client.notify.services(notify_service_sid).notifications.create(
                    body='There has been a change at: '+address,
                    to_binding=[sendTo1, sendTo2]
                )

            print("+ Notification SID: ", notification.sid)

            print("+++ Exit.")
    continue

注:请记住,如果拍卖已经过期,将有一个错误。因为它找不到span.current-bid


Tags: tofromimportbrowserurlsupportaddressselenium