搜尋

首頁  >  問答  >  主體

如何在沒有HTML元素的情況下從網站上進行資料爬取?

如何從以下網站抓取資料以查找特定案例詳細資訊?

以下是查找案例詳細資訊的手動步驟:

  1. 導覽至 https://www.claytoncountyga.gov/government/courts/court-case-inquiry/
  2. 似乎可能有一個帶有按鈕/輸入的 JavaScript 載入表單,可讓您進一步了解案例詳細資訊 - 需要選擇「姓名搜尋」以按姓氏搜尋案例 - 點擊它
  3. 然後,在 (2) 的同一元素中會出現一個新螢幕,允許使用者從下拉法院(例如治安法院)中進行選擇,並透過自由格式文字輸入來輸入姓氏和名字(Smith John)。
  4. 點擊「提交」即可查看所有案例
  5. 點擊表中與所有先前步驟相同的元素中填充的行之一上的案例編號,即可查看案例詳細資訊 - 我想從此頁面抓取資料。

因為內部表單似乎是封裝的(我猜是用 Javascript 實現的),所以我看不到提供每個輸入後呈現的 HTML 元素。我如何使用 Python 實現自動化?

P粉819533564P粉819533564330 天前558

全部回覆(1)我來回復

  • P粉458725040

    P粉4587250402024-03-21 09:39:39

    該表單包含在 ID 為「Clayton County」的 iframe 內。為了使 selenium 能夠與其中的元素交互,我們首先必須使用 EC.frame_to_be_available_and_switch_to_it 方法切換到它。

    然後使用 Select() 我們可以從下拉式選單中選擇一個選項。

    在最後一頁中,我們取得所有案例編號 url 並將它們保存在 case_numbers_urls 中,以便我們可以循環它們,載入每個案例,獲取資訊並傳遞到下一個案例。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome(service=Service(chromedriver_path))
    driver.get('https://www.claytoncountyga.gov/government/courts/court-case-inquiry/')
    
    # page 1
    wait = WebDriverWait(driver, 9)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "Clayton County")))
    driver.find_element(By.XPATH, "//a[contains(.,'Name Search')]").click()
    
    # page 2
    dropdown = wait.until(EC.element_to_be_clickable((By.ID, "ctt")))
    Select(dropdown).select_by_value('M')
    lname = 'Smith'
    fname = 'John'
    driver.find_element(By.NAME, 'lname').send_keys(lname)
    driver.find_element(By.NAME, 'fname').send_keys(fname)
    driver.find_element(By.ID, 'btnSrch').click()
    
    # page 3
    case_numbers_urls = [c.get_attribute('href') for c in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '#myTable a[href]:not([rel])')))]
    for url in case_numbers_urls:
        driver.get(url)
        # do something

    回覆
    0
  • 取消回覆