所以這是我之前的問題的延續,在 Prophet 的幫助下,我能夠讓它作業。直到上周。
基本上:在這個網站上,每個串列都有一個MLS 編號,我正試圖取消它。
現在,由于原始問題的變化主要是一種新邏輯,通過嘗試塊處理隨機彈出視窗以嘗試單擊隨機彈出視窗,如果它有效則繼續報廢,如果找不到彈出視窗則繼續報廢反正。該嘗試塊邏輯有效。但是現在當我運行代碼時,我注意到價格和 MLS 報廢回傳空結果。
我添加了一個列印宣告并確認 mls 和 price 都是空的:
mls is price is
main.py 是帶有 try 塊的代碼。 https://github.com/jzoudavy/webScrap_Selenium/blob/main/main.py
為了簡化這一點,我洗掉了 try 塊和其他額外的東西: https ://github.com/jzoudavy/webScrap_Selenium/blob/main/test.py 但運行 test.py 也證實我不再拿起價格或mls,其他一切都很好。
uj5u.com熱心網友回復:
您可以嘗試下一個示例:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)
s=Service('./chromedriver')
driver= webdriver.Chrome(service=s, options=options)
url='https://www.centris.ca/en/properties~for-sale~brossard?view=Thumbnail'
driver.get(url)
time.sleep(4)
listings = driver.find_elements(By.CLASS_NAME, 'description')
for listing in listings:
try:
price = listing.find_element(By.XPATH, './/*[@itemprop="price"]//following-sibling::span[1]').text
data_mlsnumber = listing.find_element(By.XPATH, './/*[@]').get_attribute('data-mlsnumber')
print(price , data_mlsnumber)
except:
pass
輸出:
$379,900 26194286
$529,900 23424373
$1,198,000 11994635
$345,900 23572465
$769,000 18521757
$574,000 28083515
$445,000 16204179
$329,000 14472385
$499,000 21331679
$515,000 13217312
$445,000 25003504
$799,000 9396792
$848,000 16371416
$598,000 10627269
$439,000 23978353
$419,900 16298302
$750,000 15553712
$429,000 24377480
$599,000 25292367
$1,790,000 21076634
uj5u.com熱心網友回復:
解決方案
您可以通過get_attribute()獲取廣告并獲取data-mlsnumber帖子中的值
ads = driver.find_elements(By.XPATH, "//div[@class='description']/a")
prices = driver.find_elements(By.XPATH, "//div[@class='price']/span[text()][not(@class='desc')]")
for i in range(0, len(ads)):
print(f'mls: {ads[i].get_attribute("data-mlsnumber")}, price: {prices[i].text.strip()}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/529208.html
標籤:Python硒
上一篇:FK模型中的Django_id
