Selenium of Beautiful Soupを使用して、2015年から2020年までの毎年1月の「この家」のZestimateを取得する方法はありますか?[複製]
次のリンクから、データをスクレイピングできるようにしたいと思います。しかし、Beautiful Soupを使用していると、htmlでそれを見つけることができず、BeautifulSoupが機能しませんでした。さらに、セレンを使用してこのデータを取得できるのではないかと思いましたが、このコンテンツも見つかりません。2015年から2020年までの毎年1月の「この家」のZestimateを取得するために、セレンまたは美しいスープをどのように使用するか知っていますか?よろしくお願いします。私はPythonを使用しています。
https://www.zillow.com/homedetails/1954-Sandy-Point-Ln-Mount-Pleasant-SC-29466/10938706_zpid/

回答
以下のコードを試してみてください、それは家のためのZestimateを与えます。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
options = Options()
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))
driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.get("https://www.zillow.com/homedetails/1954-Sandy-Point-Ln-Mount-Pleasant-SC-29466/10938706_zpid/")
Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
action.move_to_element(Home_Value).click().perform()
Zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[1]/div/div[1]/div/div/p').text
print(Zestimate)
について-「2015年から2020年まで毎年1月?」-1月に同じスクリプトを手動で実行して、最新のZestimateを取得できます。cronジョブを作成することもできます。しかし、私はそれを行う方法がわかりません。
PS-このスクリプトを約3〜4回実行した後、CAPTCHAに直面しています。このリンクで利用可能な良い説明があります
抽出するにはZestimateをすなわちZestimate®: $4,232,581
あなたが誘導しなければならないWebDriverWaitをするためにelement_to_be_clickable()
、あなたは、次のいずれかを使用することができますロケータ戦略:
使用
XPATH
:driver.get('https://www.zillow.com/homedetails/1954-Sandy-Point-Ln-Mount-Pleasant-SC-29466/10938706_zpid/') print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(., 'For sale')]//following::span[contains(@class, 'ds-dashed-underline') and contains(., 'Zestimate')]//ancestor::span[2]"))).text)
注:次のインポートを追加する必要があります:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC