python - beautifulsoup 找不到类

标签 python python-3.x web-scraping beautifulsoup

我尝试使用 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 过滤器,然后重新加载页面。你会看到的 提出的请求列表:

enter image description here

单击那些并查看您感兴趣的那些。找到您喜欢的那个后,复制 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/

相关文章:

Python 3 假设模块命名空间

python - 通过添加 timedelta 创建具有唯一值的 DatetimeIndex 的 DataFrame

python - 在 Python 中为 : how can I get the exit status of the previous process run from Bash (i. e。 "$?")?

python-3.x - 如何防止Elasticsearch仅对具有multi_match的一个非英语字符进行匹配

python - Scrapy 没有按照 allowed_domains 过滤结果

string - 如何将 selenium webelements 转换为 python 中的字符串列表

python - <for> 循环的切片函数

python - 让 BeautifulSoup 像浏览器一样处理换行符

python-3.x - google.cloud storage python api在指定位置创建桶

web-scraping - Dexador 没有 OpSSL 版本号