Python 获取动态表数据
我有以下代码:Python 获取动态表数据,python,parsing,web-scraping,beautifulsoup,lxml,Python,Parsing,Web Scraping,Beautifulsoup,Lxml,我有以下代码: 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')) 网页上有6个表,但只返回4个表。我尝试使用'
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'))
网页上有6个表,但只返回4个表。我尝试使用'html.parser'或'html5lib'作为解析器,但都不起作用
你知道我如何从网页上获得“扩展排名”表格吗
谢谢
请求
无法获取由JS
加载的数据。因此,您必须使用selenium
。首先通过pip
-pip安装selenium
安装selenium
,然后下载并将文件放在您的工作目录中。然后尝试以下代码
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()
请参见selenium
如果您在
Linux
上,并且得到错误Chromedriver可执行文件需要位于路径中
,那么请尝试以下方法-,其余的由JS加载。这是什么意思?您知道我如何访问它吗?您可以使用selenium访问其余部分。