검색 창에서 send_keys (Python)를 사용한 후 웹 사이트에서 검색 결과를 구문 분석하는 방법은 무엇입니까?

Jan 05 2021

내 질문은이 게시물과 관련이 있습니다. 검색 창에 검색어 입력 및 결과 스크랩

이 이전 질문에 대한 답변을 실행할 수는 있지만을 반복하여 Chrome이 탐색하는 웹 사이트에서 데이터를 긁어 낼 수는 없습니다 book. d내 코드에서 데이터를 긁어내는 방법을 보여주는 답변 만 찾았 지만을 사용한 후 검색 결과에서는 찾지 못했습니다 send_keys.

요소에 액세스하려고했지만 액세스 할 수 없으며 검색 book한 다음 루프의 다음 라운드로 이동 한 후 결과 웹 사이트에서 데이터를 긁어 내고 싶습니다 .

나는 시도했다 :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
d = webdriver.Chrome('mypath/chromedriver.exe')
books = ['9780062457738']
for book in books:
  d.get('https://www.bol.com/nl/')
  e = d.find_element_by_id('searchfor')
  f = print(e.send_keys(book, Keys.ENTER))

또한 print()함수 없이 시도했지만 입력하면 실제 요소가 반환되지 않습니다 f?.

Type:        NoneType
String form: None
Docstring:   <no docstring>

검색 쿼리를 제출 한 책의 저자, ​​제목 또는 기타 정보의 데이터를 구문 분석하는 방법에 대한 도움 은 매우 환영합니다!

답변

DebanjanB Jan 05 2021 at 22:26

즉, 추출 및 책 제목에 A ~ F * CK는 포기하지 않을의 미묘한 예술 이 유도 할 필요가 WebDriverWait을 위해를 visibility_of_element_located()하고 다음 중 하나 사용할 수 있습니다 로케이터 전략 :

  • 사용 CSS_SELECTOR:

    driver.get("https://www.bol.com/nl/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.js-confirm-button>span"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#searchfor"))).send_keys("9780062457738")
    driver.find_element_by_css_selector("button[type='submit']").click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.product-title"))).get_attribute("innerHTML"))
    
  • 사용 XPATH:

    driver.get("https://www.bol.com/nl/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Accepteren']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='searchfor']"))).send_keys("9780062457738")
    driver.find_element_by_xpath("//button[@type='submit']").click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@class, 'product-title')]"))).get_attribute("innerHTML"))
    
  • 콘솔 출력 :

    The Subtle Art of Not Giving a F*ck
    
  • 참고 : 다음 가져 오기를 추가해야합니다.

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC