【问题标题】:Convert dynamically loaded table into Pandas Dataframe将动态加载的表转换为 Pandas Dataframe
【发布时间】:2018-05-29 10:27:41
【问题描述】:

在 Windows 设备上运行 Python 3.6.1 |Anaconda 4.4.0(64 位)。

使用 selenium 我收集了以下 html 源代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = "https://nextgenstats.nfl.com/stats/receiving#yards"
driver = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/chromedriver.exe")
driver.get(url)
htmlSource = driver.page_source

如果检查 url,他们会看到一个动态加载的漂亮表格。我不确定如何从htmlsource 中提取此表,以便可以从中构造 Pandas 数据框。

【问题讨论】:

  • pandas 有read_html(),可以在文件中找到所有<table>
  • @furas read_html() without BeautifulSoup 返回一个错误,指出未找到表。 COLDSPEED 的答案有效。
  • 没有回答,只是建议使用什么。

标签: python pandas selenium dataframe html-table


【解决方案1】:

你已经很接近了。你只需要在这里帮助熊猫一点。简而言之,这就是您需要做的事情。

  1. 将源加载到BeautifulSoup
  2. 找到有问题的表。使用soup.find
  3. 致电pd.read_html
from bs4 import BeautifulSoup

soup = BeautifulSoup(htmlSource, 'html.parser')
table = soup.find('div', class_='ngs-data-table')

df_list = pd.read_html(table.prettify())

现在,df_list 包含该页面上所有表的列表 -

df_list[1].head()

                0    1   2    3    4     5      6   7    8      9     10  11
0    Antonio Brown  PIT  WR  4.3  2.6  13.7  45.32  99  160  61.88  1509   9
1  DeAndre Hopkins  HOU  WR  4.6  2.1  13.1  42.19  88  155  56.77  1232  11
2     Adam Thielen  MIN  WR  5.8  2.6  11.0  37.38  80  124  64.52  1161   4
3      Julio Jones  ATL  WR  5.2  2.4  14.2  43.34  73  118  61.86  1161   3
4     Keenan Allen  LAC  WR  5.4  2.6   9.5  31.30  83  129  64.34  1143   5

【讨论】:

  • 太棒了,BeautifulSoup 是缺少的链接。
  • @sunspots 可能还有另一种方法可以做到这一点,但据我所知,这是到目前为止最简单的方法。只需检查数据、确定表格,其余的,正如他们所说,就是历史。
  • 一两天后,我可能会悬赏这个问题,看看是否有人想分享其他技巧,使用 read_html 的众多论点中的任何一个。跨度>
  • @COLDSPEED dryscape 可能是另一种方式,但对 Windows 的支持似乎很复杂:pypi.python.org/pypi/dryscrape/1.0
【解决方案2】:

作为一个 Scrapy 用户,我习惯于查看 XHR 请求。如果您在站点中更改年份,您将看到对 https://appapi.ngs.nfl.com/statboard/receiving?season=2017&seasonType=REG 的 API 调用

API 返回 JSON,因此使用像 read_json 这样的 JSON 解析器来处理数据是有意义的。

Scrapy shell 的使用方法如下:

$ scrapy shell

In [1]: fetch("https://appapi.ngs.nfl.com/statboard/receiving?season=2017&seasonType=REG")
2017-12-15 13:11:30 [scrapy.core.engine] INFO: Spider opened
2017-12-15 13:11:31 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://appapi.ngs.nfl.com/statboard/receiving?season=2017&seasonType=REG> (referer: None)

In [2]: import pandas as pd

In [3]: data = pd.read_json(response.body)

In [4]: data.keys()
Out[4]: Index([u'season', u'seasonType', u'stats', u'threshold'], dtype='object')

In [5]: pd.DataFrame(list(data['stats']))

如果没有scrapy,可以使用requests

import requests
import pandas as pd

url = "https://appapi.ngs.nfl.com/statboard/receiving?season=2017&seasonType=REG"

response = requests.get(url)
data = pd.read_json(response.text)
df = pd.DataFrame(list(data['stats']))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 2017-03-17
    • 2017-03-23
    • 1970-01-01
    • 2017-04-13
    • 2021-03-29
    • 2017-04-27
    相关资源
    最近更新 更多