如何让脚本使用Selenium为多个URL运行相同的过程

时间:2019-05-08 18:55:06

标签: python python-3.x selenium

我想做的是让硒在不同的URL上执行相同的过程。我的脚本所做的是转到网址(这是一个房地产拍卖网站)并监控价格,并通过文本(twilio)通知我任何更改。现在,它一次可用于1个物业。现在,我需要它同时对多个网址执行相同的过程。

这是我所有的代码:

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.common.exceptions import NoSuchElementException
import ssl
from twilio.rest import Client
from twilio.rest import TwilioRestClient

browser = webdriver.Chrome()
urls = [
    'https://www.hubzu.com/property/0007190806526-3925-E-Landis-Ave-Vineland-NJ-08361',
    'https://www.hubzu.com/property/0007192328826-100-Melissa-Ln-Headland-AL-36345'
]
while True:
    browser.get(('https://www.hubzu.com/property/9007091467618-3632-Stokes-Drive-Sarasota-FL-34232'))# Live Auction Bid URL
    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

1 个答案:

答案 0 :(得分:0)

容易,只要遍历所需的URL列表即可,只要您想在每个URL上执行相同的操作即可。

urlList = ["your first URL here", "your second URL here", ...]

for url in urlList

    # your setup code here

    browser.get(url)

    # all the other stuff you want to do here

如果您确实真的想同时执行多个循环,只需启动该程序几次,也许将URL作为命令行参数传入(删除URL循环),然后让您的机器来处理它即可。