2017-08-24 11 views
1

をフェッチI次のコードを持っている:美しいスープは、動的テーブルのデータ

url = 'https://www.basketball-reference.com/leagues/NBA_2017_standings.html#all_expanded_standings' 
html = urlopen(url) 
soup = BeautifulSoup(html, 'lxml') 

print(len(soup.findAll('table'))) 
print(soup.findAll('table')) 

は、Webページ上の6つのテーブルがありますが、それは唯一の4つのテーブルを返します。私はパーサとして 'html.parser'または 'html5lib'を使用しようとしましたが、どちらも動作しませんでした。

どのようにしてウェブページからテーブル「展開順位」を得ることができますか?

ありがとうございます!

+0

残りはJSによってロードされています。 –

+0

どういう意味ですか?私はそれにどのようにアクセスできるのか知っていますか? – user2993519

+0

残りはセレンでアクセスできます。 – SIM

答えて

1

requestsJSでロードされたデータをフェッチできません。したがって、seleniumを使用する必要があります。最初にseleniumpippip install seleniumでインストールし、chrome driverをダウンロードして作業ディレクトリに置きます。次に、次のコードを試してください。

from bs4 import BeautifulSoup 
import time 
from selenium import webdriver 

url = "https://www.basketball-reference.com/leagues/NBA_2017_standings.html" 
browser = webdriver.Chrome() 

browser.get(url) 
time.sleep(3) 
html = browser.page_source 
soup = BeautifulSoup(html, "lxml") 

print(len(soup.find_all("table"))) 
print(soup.find("table", {"id": "expanded_standings"})) 

browser.close() 
browser.quit() 

documentationを参照してください。

あなたがLinux上にあり、これらの方法を試してください、次のエラーChromedriver executable needs to be in the PATHを取得した場合 - link-1link-2

関連する問題