试图抓取维基百科页面,我之前做了很多次
library(XML)
myURL <- "http://en.wikipedia.org/wiki/List_of_US_Open_MenUs_Singles_champions"
y <- readHTMLTable(myURL, stringsAsFactors = FALSE)
R在RStudio或标准GUI中崩溃
关于类似问题的其他SO评论建议使用readLines
u=url(myURL)
readLines(u) # cannot open: HTTP status was '404 Not Found'
该网址实际上已重定向,因此输入了最终网址
myURL <- "http://en.wikipedia.org/wiki/List_of_US_Open_Men%27s_Singles_champions"
这次readLines会输出页面但是使用XML函数(包括htmlParse)仍会导致崩溃
TIA
答案 0 :(得分:3)
我发现软件包httr
在解决任何网络抓取问题方面都非常宝贵。在这种情况下,您需要添加用户代理配置文件,因为如果您不这样做,Wikipedia会阻止内容:
library(httr)
library(XML)
myURL <- "http://en.wikipedia.org/wiki/List_of_US_Open_Men%27s_Singles_champions"
page <- GET(myURL, user_agent("httr"))
x <- readHTMLTable(text_content(page), as.data.frame=TRUE)
head(x[[1]])
产生这个:
US Open Men's Singles Champions NA
1 Official website <NA>
2 Location Queens – New York City United States
3 Venue USTA Billie Jean King National Tennis Center
4 Governing body USTA
5 Created 1881 (established)Open Era: 1968\n(44 editions, until 2011)
6 Surface Grass (1881–1974)HarTru (1975–1977)DecoTurf (1978–Present)