【问题标题】:beautifulsoup could not find classBeautifulsoup 找不到课程
【发布时间】:2017-01-20 11:02:18
【问题描述】:

我尝试使用 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')

【问题讨论】:

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


    【解决方案1】:

    网站使用ajax加载数据,而您仅通过BeautifulSoup获取页面内容将无法获得这些数据。但是,您可能根本不需要 BeautifulSoup。

    如果您使用的是 Chrome,请访问该网站并转至浏览器的开发工具,点击网络选项卡,点击 XHR 过滤器,然后重新加载页面。你会看到的 提出的请求列表:

    点击那些并查看您感兴趣的。找到您喜欢的后,复制 url,并使用 requests 库获取数据(您已将其包含在代码中):

    r = requests.get('http://stats.nba.com/stats/commonallplayers?IsOnlyCurrentSeason=0&LeagueID=00&Season=2016-17')
    data = r.json()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 2016-12-02
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      相关资源
      最近更新 更多