网页抓取 (Selenium) - 仅显示 2019 年春季,每次显示不同的结果

Web Scraping (Selenium)- only Spring 2019 shows up, and different results show up each time

提问人:Sirisha Velavan 提问时间:11/5/2023 更新时间:11/5/2023 访问量:13

问:

有时,如果搜索引擎的输入只是主题,它会打印出讲师的输出。有时它只是输入数字。但我的主要问题是它一直在打印 2019 年春季学期而不是 2024 年春季学期的讲师。我什至尝试在其上使用 selects 和 ActionChains 来让它选择下拉菜单,但它似乎总是正确地执行 2 个中的 3 个。请帮我:)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select

import time

instructor_seats = {}

#INPUTS
subject = input("What subject?")
number = input("What number?")

driver = webdriver.Chrome()

#URL
driver.get("https://catalog.apps.asu.edu/catalog/classes")

#SUBJECT AND COURSE NUM
search = driver.find_element(By.NAME, "subject")
num = driver.find_element(By.NAME, "catalogNbr")

search.send_keys(subject)
search.send_keys(Keys.RETURN)
search.clear()

num.send_keys(number)
num.send_keys(Keys.RETURN)
num.clear()




try:
    class_results = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "class-results"))
    )
    
    instructors = driver.find_elements(By.XPATH, "//div[contains(@class, 'class-results-cell instructor')]/a")
    open_seats = driver.find_elements(By.XPATH, "//div[contains(@class, 'class-results-cell seats')]")
    time_start = driver.find_elements(By.XPATH, "//div[contains(@class, 'class-results-cell') and contains(@class, 'pull-left') and contains(@class, 'days')]/p")
    

    for instructor, seats, times in zip(instructors, open_seats, time_start):
        instructor_name = instructor.text
        seat = seats.text
        tim = times.text
        key = instructor_name + " " + tim
        instructor_seats[key] = seat[:2]

    for key, value in instructor_seats.items():
        print(f"{key}: {value}")

except:
    driver.quit()

driver.quit()

selenium-webdriver 选择 webdriver

评论


答: 暂无答案