我尝试使用 bs4 从一个 NBA 统计网站获取表格。
该网站似乎没有使用 JavaScript。
soup.prettify
打印结果看起来很正常,但我无法使用 soup.find_all
获取我想要的表。这是我正在使用的代码:
import requests
from bs4 import BeautifulSoup
url = 'http://stats.nba.com/team/#!/1610612738/stats/'
page = requests.get(url)
html = page.content
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all('table')
最佳答案
该网站使用 ajax 加载数据,而仅通过使用 BeautifulSoup 获取页面内容,您将无法获得此数据。但是,您可能根本不需要 BeautifulSoup。
如果您使用的是 Chrome,请访问网站并转到浏览器的开发工具,点击网络选项卡,点击 XHR 过滤器,然后重新加载页面。你会看到的 提出的请求列表:
单击那些并查看您感兴趣的那些。找到您喜欢的那个后,复制 url,并使用请求库获取数据(您已经将其包含在您的代码中):
r = requests.get('http://stats.nba.com/stats/commonallplayers?IsOnlyCurrentSeason=0&LeagueID=00&Season=2016-17')
data = r.json()
关于python - beautifulsoup 找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459947/