【问题标题】:Web Scraping NBA Fantasy Projections - R网页抓取 NBA Fantasy Projections - R
【发布时间】:2016-11-14 06:22:32
【问题描述】:

我想以更精简的方式整理出许多 NBA Fantasy Projections。目前我在谷歌表格中使用 importhtml 函数和简单的古老的 cut'n'paste 的组合。

我经常使用 R 从互联网上抓取其他数据,但是,我无法设法抓取这些表格。我遇到问题的表格位于三个不同的地址(每页 1 个表格),它们是:

1) http://www.sportsline.com/nba/player-projections/player-stats/all-players/

2) https://swishanalytics.com/optimus/nba/daily-fantasy-projections

3)http://www.sportingcharts.com/nba/dfs-projections/

对于我所有其他的抓取活动,我使用包 rvest 和 xml。按照相同的过程,我尝试了下面列出的两种方法,结果显示了输出。我确定这与网站上表格的格式有关,但是我找不到可以帮助我的东西。


方法一

library(XML)
projections1 <- readHTMLTable("http://www.sportsline.com/nba/player-projections/player-stats/all-players/")
projections2 <- readHTMLTable("https://swishanalytics.com/optimus/nba/daily-fantasy-projections")
projections3 <- readHTMLTable("http://www.sportingcharts.com/nba/dfs-projections/")

输出

projections1    
named list()

projections2
named list()
Warning message:
XML content does not seem to be XML: 'https://swishanalytics.com/optimus/nba/daily-fantasy-projections'

projections3 - 我得到了表格的标题,但没有得到表格的内容。

方法二

library(rvest)
URL <- "http://www.sportsline.com/nba/player-projections/player-stats/all-players/"
projections1 <- URL %>%  
    read_html %>% 
    html_nodes("table")  %>% 
    html_table(trim=TRUE,fill=TRUE)

URL <- "https://swishanalytics.com/optimus/nba/daily-fantasy-projections"
projections2 <- URL %>%  
    read_html %>% 
    html_nodes("table")  %>% 
    html_table(trim=TRUE,fill=TRUE)

URL <- "http://www.sportingcharts.com/nba/dfs-projections/"
projections3 <- URL %>%  
    read_html %>% 
    html_nodes("table")  %>% 
    html_table(trim=TRUE,fill=TRUE)

输出

projections1    
list()

projections2 - 我得到了表格的标题,但没有得到表格的内容。

projections3 - 我得到了表格的标题,但没有得到表格的内容。


如果有人能指出我正确的方向,将不胜感激。

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    表格的内容是javascript生成的,所以readHTMLTableread_html什么都找不到,可以找到如下表格

    projections1:link

    import requests 
    url = 'http://www.sportsline.com/sportsline-web/service/v1/playerProjections?league=nba&position=all-players&sourceType=FD&game=&page=PS&offset=0&max=25&orderField=&optimal=false&release2Ver=true&auth=3'
    r = requests.get(url)
    print r.json()
    

    projections2: view-source:https://swishanalytics.com/optimus/nba/daily-fantasy-projections Line 1181

    import requests   
    url = 'https://swishanalytics.com/optimus/nba/daily-fantasy-projections'
    r = requests.get(url)
    text = r.content
    print eval(text.split('this.players = ')[1].split(';')[0])
    

    projections3:查看源代码第 918 行

    【讨论】:

    • 哇,现在这很酷。老实说,我以前不知道视图源。
    • 虽然,我不知道如何从网站源中抓取,你能给我一些指导吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2020-07-20
    • 2011-08-15
    相关资源
    最近更新 更多