2016-03-25 10 views
-1

私はGhostとBeautifulSoupを使ってHTMLページを解析しています。私が持っている問題は、このページの内容が動的であることです(angularJSで作成されています)。最初は、htmlは "please wait!page loading"のようなものしか表示しません。数秒後に、htmlの内容が表示されます。 GhostとBeatifulSoupの使用私はちょうど2つの小さいdivsを持っているローディングページのHTMLコードを得ます。 URLは同じです。 「本当の」コンテンツが読み込まれるまで待つ可能性はありますか?Pythonを使用して動的ページを解析するにはどうすればよいですか?

答えて

1

を参照してください:

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

driver = webdriver.PhantomJS() 
driver.get("your url here") 

# waiting for the page to load - TODO: change 
wait = WebDriverWait(driver, 10) 
wait.until(EC.visibility_of_element_located((By.ID, "content"))) 

data = driver.page_source 
driver.close() 

soup = BeautifulSoup(data, "html.parser") 
2

phantomjsを使用してページを開きます。 phantomjsファイルシステムモジュールAPIを使用してローカルファイルとして保存します。 後でこのローカルファイルハンドルを使用してBeautifulSoupオブジェクトを作成し、ページを解析します。 .page_sourceを取得し、BeautifulSoupに渡し、表示されるように所望のコンテンツのためseleniumによって自動化された実際のブラウザ(PhantomJSのようなヘッドレスでもオプションです)、waitでページをロードしhttp://www.kochi-coders.com/2014/05/06/scraping-a-javascript-enabled-web-page-using-beautiful-soup-and-phantomjs/

関連する問題