我可能會不喜歡它alecxe suggested,但我會使用加載定義本身的URL。例如,搜索azul
:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('http://lema.rae.es/drae/srv/search?val=azul')
print driver.find_element_by_css_selector("body>div").text
出現在問題中的URL加載一個頁面,然後加載定義的URL在iframe
元素。直接使用上面顯示的URL加載定義會節省一些工作量和一些複雜性:整個定義包含在body
的第一個div
孩子中。不幸的是,它並沒有消除對JavaScript的需求。
運行上面代碼:
azul.
(Quizá alterac. del ár. hisp. lazawárd, este del ár. lāzaward, este del persa laǧvard o lažvard, y este del sánscr. rājāvarta, rizo del rey).
1. adj. Del color del cielo sin nubes. Es el quinto color del espectro solar. U. t. c. s.
2. m. El cielo, el espacio. U. m. en leng. poét.
3. m. Méx. Miembro del cuerpo de Policía.
~ de cobalto.
[... etc ...]
請注意,我還沒有發現需要使用任何等待機制來檢測該頁面的內容已準備就緒。在調試器中查看頁面a)我沒有看到任何Ajax請求,並且b)查看JavaScript和頁面本身,它看起來像是一個混淆頁面,JavaScript隨後同步去混淆。所以在driver.get
返回時,內容應該準備好被使用。
當我嘗試執行驅動程序= webdriver.PhantomJS()時出現錯誤: selenium.common.exceptions.WebDriverException:消息:無法使用ghostdriver啓動phantomjs。 屏幕截圖:可通過屏幕 – 2015-03-19 13:20:17
@franciscosollima以及請參閱http://superuser.com/questions/674322/python-selenium-phantomjs-unable-to-start-phantomjs-with-ghostdriver和http://stackoverflow.com/問題/ 16389938 /使用硒合的背景。首先,將'selenium'升級到最新版本:'pip install --upgrade selenium'。另外,下載'phantojs'可執行並通過'driver = webdriver.PhantomJS('path_here')'啓動它。希望有所幫助。 – alecxe 2015-03-19 13:32:07
好吧,我必須在幾個小時內嘗試它,對於延遲抱歉。 – 2015-03-19 13:40:03