简体   繁体   English

让Selenium对多个URL执行相同的过程

[英]Have Selenium Do the same process for multiple URLS

I created a script that goes to a property auction site that monitors the price and alerts me of any of the changes. 我创建了一个脚本,该脚本转到房地产拍卖网站,该网站监视价格并向我发出任何更改的警报。 I originally made it to monitor 1 url at a time. 我最初是一次监视1个URL。 I now need it to monitor more then 1 url and do the same exact process at the same time. 现在,我需要它来监视超过1个网址,并同时执行相同的确切过程。

I've tried looping the urls in browser.get but that doesn't seem to work for me(idk if i wrote it wrong, probably did). 我试过循环在browser.get中的url,但这似乎对我不起作用(如果我写错了,idk可能就可以了)。 But i need it to either, open a tun of new tabs and go to the urls, or another way for it to monitor everything. 但是我需要它来打开大量新标签并转到url,或者用另一种方式来监视所有内容。

The Original Script for 1 property url, works perfectly, And the only difference between that and the code below is browser.get . 用于1个属性url的原始脚本可以完美运行,并且它与下面的代码之间的唯一区别是browser.get

PS: If script is run, and auction urls are expired (meaning theres no price it can look at) there will be a error PS:如果运行脚本,并且拍卖网址已过期(这意味着它无法查看价格),将会出现错误

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

Your code seems to be mostly fine and cycles through them for me. 您的代码似乎很好,并且对我来说遍历它们。 Unfortunately all the properties are not for sale, so they all give me errors and I cannot produce the expected behaviour. 不幸的是,所有财产都不出售,因此它们都会给我带来错误,并且我无法产生预期的行为。 I think you should implement a try: and except: , the finally: runs regardless of what happened, and I wouldn't imagine you want an update every 2 seconds XD. 我认为您应该实现try:except: :,而不是final finally:不管发生了什么,都可以运行,而且我无法想象您要每隔2秒XD更新一次。 You also don't need continue at the end, it does nothing. 您也不需要最后continue ,它什么也没做。 The code 编码

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
        try:
            propertyprice = browser.find_element_by_css_selector('span.current-bid')
            currentBidText = propertyprice.text
            WebDriverWait(browser, 90000).until(
                EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'span.current-bid'), currentBidText)
                )
        except Exception as e:
            print("an error occurred on property {}:\n{}".format(address.strip(), e))
        finally:
            pass
            # 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.")

Does cycle through all the properties, and a sample output for me is: 遍历所有属性,对我来说,示例输出是:

an error occurred on property 730
Trillium Ln

 Lilburn, GA 30047:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"span.current-bid"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

an error occurred on property 225 Marriott Ave

 Schenectady, NY 12304:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"span.current-bid"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

an error occurred on property 17 Spring Meadows Dr

 Ormond Beach, FL 32174:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"span.current-bid"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

an error occurred on property 730
Trillium Ln

 Lilburn, GA 30047:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"span.current-bid"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

an error occurred on property 225 Marriott Ave

 Schenectady, NY 12304:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"span.current-bid"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.14393 x86_64)

and so on in an infinite loop... 等等无限循环...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM