【问题标题】:ValueError: no tables foundValueError:找不到表
【发布时间】:2020-09-11 11:43:59
【问题描述】:

好的,对你们中的一些人来说应该很容易,但我找不到解决我这个问题的任何方法。我正在尝试从 NHL.com 上抓取一张表格,但它找不到任何表格 - 我尝试了“table”、“ReactTable”等任何东西。

import pandas as pd
import requests
import urllib.request
import time
from bs4 import BeautifulSoup
from pandas_profiling import ProfileReport
import numpy as np
import matplotlib as plt
url = "http://www.nhl.com/stats/teams?report=goalsbyperiod&reportType=game&dateFrom=2020-03-11&dateTo=2020-09-11&gameType=3&filter=gamesPlayed,gte,1&sort=period1GoalsFor&page=0&pageSize=50"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
result = soup.find_all('table')
df_raw = pd.read_html(str(table))[0]

它只显示 ValueError: no tables found

相同的代码适用于我使用的大多数网站。请帮忙。

【问题讨论】:

  • 页面有javascript在页面显示后加载数据...你需要从运行JS的浏览器实例开始,然后报废数据!

标签: python html pandas beautifulsoup python-requests


【解决方案1】:

页面上的数据在div 而不是table 中。您可以为此使用selenium

from selenium import webdriver
import os
import time

chrome_driver = os.path.abspath(os.path.dirname(__file__)) + '/chromedriver'
browser = webdriver.Chrome(chrome_driver)
url = 'http://www.nhl.com/stats/teams?report=goalsbyperiod&reportType=game&dateFrom=2020-03-11&dateTo=2020-09-11&gameType=3&filter=gamesPlayed,gte,1&sort=period1GoalsFor&page=0&pageSize=50'

browser.get(url)

time.sleep(3)

data = browser.find_element_by_class_name('rt-tbody').text

输出:

1
Colorado Avalanche
15
9
5
--
--

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 2019-04-23
    • 2020-05-29
    • 2014-12-03
    • 2017-02-21
    • 2021-11-21
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多