我想用R從a website中抓取數據。我使用rvest
試圖模仿an example scraping the IMDB page for the Lego Movie。該示例主張使用名爲Selector Gadget的工具來幫助輕鬆識別與您要提取的數據相關聯的html_node
。用R和選擇器小工具進行網絡抓取
我在構建具有以下架構/列的數據幀最終感興趣: rank
,blog_name
,facebook_fans
,twitter_followers
,alexa_rank
。
我的代碼如下。我能夠使用Selector Gadget正確識別Lego示例中使用的html標籤。但是,遵循與樂高相同的流程和相同的代碼結構,我得到了NAs(...using firstNAs introduced by coercion[1] NA
)。我的代碼如下:
data2_html = read_html("http://blog.feedspot.com/video_game_news/")
data2_html %>%
html_node(".stats") %>%
html_text() %>%
as.numeric()
我也試驗過:html_node("html_node(".stats , .stats span"))
,這似乎爲「臉書粉絲團」欄目工作,因爲它報告714場比賽,但只返回返回1號。
714 matches for .//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' stats ')] | .//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' stats ')]/descendant-or-self::*/span: using first{xml_node}
<td>
[1] <span>997,669</span>
看起來非常酷,但我無法複製您的結果。錯誤:'game_blogs <- h %>% html_node('table')%>%#選擇包含表節點 html_table()%>%#將錶轉換爲data.frame set_names(make.names) 錯誤:'x'和' nm'必須是相同的長度' – user2205916
啊!對不起,這是使用'purrr :: set_names'的開發版本,它可以使用一個函數。你可以從[Github](https://github.com/tidyverse/purrr/)安裝它,或者使用'set_names(make.names(names(。)))'來執行相同的操作。 – alistaire