일부 데이터를 긁어 모으는 웹 사이트를 발견했습니다. 그러나이 사이트는 필자의 제한된 Python 지식으로는 쓸모없는 것으로 보인다. driver.find_element_by_xpath를 사용할 때 나는 보통 타임 아웃 예외를 발생시킵니다.파이썬 셀레늄 웹 스크랩 - 데이터를 가져올 수 없습니다.
아래 제공된 코드를 사용하여 첫 번째 결과를 클릭하고 새 페이지로 이동합니다. 새 페이지에서 제품 제목 및 패키지 크기를 다 쳤습니다. 그러나 아무리 시도해도 파이썬이 나에게 맞는 것을 클릭 할 수는 없다. 데이터를 고칠 수는 없습니다. 누군가 도울 수 있습니까?
내 원하는 출력은 :
트리스 (트리 페닐 포스 핀) 로듐 (I) 클로라이드, 98 % 190,420,010
1 GR 87.60
5 GR 367.50
이들은 I 가지고있는 코드는 현재까지 :
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "http://www.acros.com/"
cas = "14694-95-2" # need to select for the appropriate one
driver = webdriver.Firefox()
driver.get(url)
country = driver.find_element_by_name("ddlLand")
for option in country.find_elements_by_tag_name("option"):
if option.text == "United States":
option.click()
driver.find_element_by_css_selector("input[type = submit]").click()
choice = driver.find_element_by_name("_ctl1:DesktopThreePanes1:ThreePanes:_ctl4:ddlType")
for option in choice.find_elements_by_tag_name("option"):
if option.text == "CAS registry number":
option.click()
inputElement = driver.find_element_by_id("_ctl1_DesktopThreePanes1_ThreePanes__ctl4_tbSearchString")
inputElement.send_keys(cas)
driver.find_element_by_id("_ctl1_DesktopThreePanes1_ThreePanes__ctl4_btnGo").click()
을 장기적으로 나는 것 한 무리의 cas를 가져 와서 결과를 출력하는 for 루프를 작성하면 자동화를위한 코드가 일반화되기를 바란다 – user3788728
일단 다른 페이지로 이동하면 (일반적으로 'click '메소드), 메모리에있는 이전의 모든 요소는 잠재적으로 유효하지 않을 수 있습니다 (일명 "부실"). 코드의 각 for 루프에서이 메소드를 호출 한 후에'break'를 추가하는 것이 좋습니다. –