Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R从Web上抓取列_R_Web Scraping_Rvest - Fatal编程技术网

使用R从Web上抓取列

使用R从Web上抓取列,r,web-scraping,rvest,R,Web Scraping,Rvest,我有这个网站,我想提取前4列。但它不起作用。我是网络垃圾的乞丐,任何帮助都会是了不起的家伙: https://projects.fivethirtyeight.com/2017-nba-predictions/ 我想提取每一列:ELO--CARM-ELOe等等 这就是我到目前为止所做的: url_nba <- 'https://projects.fivethirtyeight.com/2017-nba-predictions/' webpage_nba <- read_html(

我有这个网站,我想提取前4列。但它不起作用。我是网络垃圾的乞丐,任何帮助都会是了不起的家伙:

https://projects.fivethirtyeight.com/2017-nba-predictions/
我想提取每一列:
ELO--CARM-ELO
e等等

这就是我到目前为止所做的:

url_nba <- 'https://projects.fivethirtyeight.com/2017-nba-predictions/'

webpage_nba <- read_html(url_nba)

data_nba.1 <- html_nodes(webpage_nba,'.num elo original desktop')
data_nba.2 <- html_nodes(webpage_nba,'.num elo carmelo')

url\u nba查看HTML代码,表格的形状有点不正确。一种方法是抓取整个表,然后收集Elo分数

查找css标记“table”时,发现了三个表。手动查看每一个,表3是感兴趣的一个

library(rvest)
url_nba <- 'https://projects.fivethirtyeight.com/2017-nba-predictions/'
webpage_nba <- read_html(url_nba)

#collect the tables from the page
tables <- html_nodes(webpage_nba,'table')

#Process the table of interest (returns a list of 1)
resultdf <- tables[3] %>% html_table(fill=TRUE)
resultdf <- resultdf[[1]]
库(rvest)

url_nba当您获得等长的节点列表时,您可以使用css选择器来定位您想要的列,然后cbind转换为dataframe作为替代。下面的选择器将引出一个干净的输出数据帧

library(rvest)
library(magrittr)

page <- read_html('https://projects.fivethirtyeight.com/2017-nba-predictions/')
df <- setNames(data.frame(cbind(
  html_text(html_nodes(page, 'td.original')),
  html_text(html_nodes(page, 'td.carmelo')),
  html_text(html_nodes(page, '.change')),
  html_text(html_nodes(page, '.team a'))
)),c('elo','carmelo','1wkchange','team'))

print(df)
库(rvest)
图书馆(magrittr)

页面上你说的前四栏将是elo、carmelo、1周变更和团队徽标。真的是这样吗?如果是这样的话,您希望如何处理徽标?非常感谢!你的意思是什么:
HTML代码表格的形状有点不正确
?如果你查看resultdf,前两行是空的,列标题在第3行。如果你看一下桌子,所有这些都很容易解决。非常感谢!但我有一个从未有过的问题:为什么与COLLMN
elo
相关的标记是
td.original
而不是
。num elo original desktop
我缺少什么?num elo original desktop是一个多值类属性,即类属性有多个值。这是更脆弱的,因为更大的机会,一个或多个可以改变,也是更多的价值观相匹配,所以潜在的速度较慢-所以你想;t-sholer选择器;包括在可能的情况下使用单个多值类(仔细选择)。最后,它在th和td上匹配。我只想用最短、最快的选择器匹配实际的td元素。td.original唯一标识的感兴趣元素和类css选择器是第二快的(仅次于id)。我没有单词!谢谢我从R开始抓网。如果我想提高我的技能,你有什么建议吗?学习HTML语言会对我有帮助吗?了解基本的HTML和css选择器,包括高级选择器会很有帮助。如果您访问我的个人资料,您将在“关于”部分看到许多共享的链接。