2014-07-21 19 views
0

일부 데이터를 긁어 모으는 웹 사이트를 발견했습니다. 그러나이 사이트는 필자의 제한된 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() 
+0

을 장기적으로 나는 것 한 무리의 cas를 가져 와서 결과를 출력하는 for 루프를 작성하면 자동화를위한 코드가 일반화되기를 바란다 – user3788728

+0

일단 다른 페이지로 이동하면 (일반적으로 'click '메소드), 메모리에있는 이전의 모든 요소는 잠재적으로 유효하지 않을 수 있습니다 (일명 "부실"). 코드의 각 for 루프에서이 메소드를 호출 한 후에'break'를 추가하는 것이 좋습니다. –

답변

0

제시된 코드가 작동합니다. Firefox의 인스턴스를 검색 결과를 보여주는 http://www.acros.com/DesktopModules/Acros_Search_Results/Acros_Search_Results.aspx?search_type=CAS&SearchString=14694-95-2으로 지정한다는 점에서 저에게 좋습니다.

해당 페이지에 iframe 요소 찾을 경우

<은 iframe 아이디 = "searchAllFrame"allowtransparency = ""배경 색 = "투명"FRAMEBORDER = "0"폭 = "1577"높이 = "3000 "스크롤 ="자동 "src ="http://newsearch.chemexper.com/misc/hosted/acrosPlugin/center.shtml?query=14694-95-2 & 검색 유형 = cas & 통화 = & country = NULL & language = EN & forGroupNames = AcrosOrganics, FisherSci, MaybridgeBB, BioReagents, FisherLCMS & 서버 = www.acros.com "> </iframe >

예를 들어, 다음 내가 생각하는 그 프레임에 당신이 거기에서 scrapable해야 할 데이터를 전환 할 driver.switch_to.frame를 사용

driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id='searchAllFrame']")) 

그런 다음 찾을 평소와 같이 드라이버를 사용하여 수행 할 수 있습니다 해당 iframe 내의 요소 (나는 switch_to_frame 비슷하게 작동하지만 사용되지 않습니다 생각합니다.)

(나는 switch_to에 대한 문서에 괜찮은 링크를 찾을 수없는 것, this이 모두 도움이되지 않습니다.

+0

안녕하세요. 내가 어떻게하는지 자세히 설명해 봤어? 난 switch_to를 한번도 사용하지 않았고, s에 관해 많은 것을 이해하지 못했다. 프레임을 마녀. 나는 약간의 연구를 할 것이다 – user3788728

+0

도움을 주셔서 대단히 감사합니다! 프레임 전환에 대해서는 전혀 몰랐습니다. 나는 driver.switch_to_frame !!!!!를 호출하여 문제를 해결할 수 있었다. – user3788728

관련 문제