आर

2012-07-29 8 views
12

में लाइव ओलंपिक पदक डेटा डाउनलोड करना ऐसा लगता है कि वेबसाइट कर्ल से सीधे पहुंच को अवरुद्ध कर रही है।आर

library(XML) 
library(RCurl) 
theurl <- "http://www.london2012.com/medals/medal-count/" 
page <- getURL(theurl) 

page # fail 
[1] "<HTML><HEAD>\n<TITLE>Access Denied</TITLE>\n</HEAD><BODY>\n<H1>Access Denied</H1>\n \nYou don't have permission to access \"http&#58;&#47;&#47;www&#46;london2012&#46;com&#47;medals&#47;medal&#45;count&#47;\" on this server.<P>\nReference&#32;&#35;18&#46;358a503f&#46;1343590091&#46;c056ae2\n</BODY>\n</HTML>\n" 

चलो यह देखने का प्रयास करें कि हम इसे सीधे तालिका से एक्सेस कर सकते हैं या नहीं।

page <- readHTMLTable(theurl) 

वहाँ Error in htmlParse(doc) : error in creating parser for http://www.london2012.com/medals/medal-count/

कैसे आप आर में इस तालिका करने के बारे में जाना होगा नहीं किस्मत?


अद्यतन: टिप्पणियों और टोइंग के जवाब में, उपयोगकर्ता एजेंट स्ट्रिंग को सामग्री प्राप्त करने के लिए काम करना। लेकिन readHTMLtable एक त्रुटि देता है।

page <- getURLContent(theurl, useragent="Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2") 
+0

लिंक्स, साथ ही अवरुद्ध हो रहा है। –

+0

चूंकि पेज फ़ायरफ़ॉक्स में लोड होता है, स्रोत देखें और डिस्क पर सहेजें? –

+0

getURL के साथ आप एक झूठी उपयोगकर्ता एजेंट स्ट्रिंग निर्दिष्ट कर सकते हैं, जो डेटा प्राप्त करने के लिए काम करता है। लेकिन readHTMLTable अभी भी अच्छी तरह से बाहर नहीं निकलता है। यह एक त्रुटि देता है ('नामों में त्रुटि (उत्तर) = शीर्षलेख: 'नाम' विशेषता [13] वेक्टर [7]') के समान लंबाई होनी चाहिए, यह सुनिश्चित नहीं है कि इसे कैसे डिबग करना है। –

उत्तर

12

ऐसा लगता है कि यह काम करता है जैसे:

rr <- readHTMLTable(page,header=FALSE) 
rr2 <- setNames(rr[[1]], 
       c("rank","country","gold","silver","bronze","junk","total")) 
rr3 <- subset(rr2,select=-junk) 
## oops, numbers all got turned into factors ... 
tmpf <- function(x) { as.numeric(as.character(x)) } 
rr3[,-2] <- sapply(rr3[,-2],tmpf)    
head(rr3) 
## rank        country gold silver bronze total 
## 1 1    People's Republic of China 6  4  2 12 
## 2 2    United States of America 3  5  3 11 
## 3 3         Italy 2  3  2  7 
## 4 4      Republic of Korea 2  1  2  5 
## 5 5         France 2  1  1  4 
## 6 6 Democratic People's Republic of Korea 2  0  1  3 
with(rr3,dotchart(total,country)) 
+0

का पालन करने के लिए मुझे लगता है आप 'readHTMLTable' कॉल में 'स्ट्रिंग्सएफ़ैक्टर्स = FALSE' का उपयोग कर सकते हैं। – GSee

+0

ठीक है, लेकिन मुझे लगता है कि मुझे अभी भी उन स्तंभों को संख्यात्मक रूप से परिवर्तित करना होगा? –

+0

क्या आपने यह देखने के लिए कोड को देखा था कि क्या यह थाड था? –

12

यहाँ मैं नियमित अभिव्यक्ति का उपयोग कर के साथ आया है। अन्य उत्तर में readHTMLTable का उपयोग करने से बहुत विशिष्ट और निश्चित रूप से बेहतर नहीं है। अधिक दिखाने के लिए कि आप आर में textmining साथ काफी दूर तक जा सकते हैं:

# file <- "~/Documents/R/medals.html" 
# page <- readChar(file,file.info(file)$size) 

library(RCurl) 
theurl <- "http://www.london2012.com/medals/medal-count/" 
page <- getURLContent(theurl, useragent="Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2") 


# Remove html tags: 
page <- gsub("<(.|\n)*?>","",page) 
# Remove newlines and tabs: 
page <- gsub("\\n","",page) 

# match table: 
page <- regmatches(page,regexpr("(?<=Total).*(?=Detailed)",page,perl=TRUE)) 

# Extract country+medals+rank 
codes <-regmatches(page,gregexpr("\\d+[^\\r]*\\d+",page,perl=TRUE))[[1]] 
codes <- codes[seq(1,length(codes)-2,by=2)] 

# Extract country and medals: 
Names <- gsub("\\d","",codes) 
Medals <- sapply(regmatches(codes,gregexpr("\\d",codes)),function(x)x[(length(x)-2):length(x)]) 

# Create data frame: 
data.frame(
    Country = Names, 
    Gold = as.numeric(Medals[1,]), 
    Silver = as.numeric(Medals[2,]), 
    Bronze = as.numeric(Medals[3,])) 

और उत्पादन:

        Country Gold Silver Bronze 
1    People's Republic of China 6  4  2 
2    United States of America 3  5  3 
3         Italy 2  3  2 
4      Republic of Korea 2  1  2 
5         France 2  1  1 
6 Democratic People's Republic of Korea 2  0  1 
7        Kazakhstan 2  0  0 
8        Australia 1  1  1 
9         Brazil 1  1  1 
10        Hungary 1  1  1 
11       Netherlands 1  1  0 
12      Russian Federation 1  0  3 
13        Georgia 1  0  0 
14       South Africa 1  0  0 
15         Japan 0  2  3 
16       Great Britain 0  1  1 
17        Colombia 0  1  0 
18         Cuba 0  1  0 
19         Poland 0  1  0 
20        Romania 0  1  0 
21    Taipei (Chinese Taipei) 0  1  0 
22        Azerbaijan 0  0  1 
23        Belgium 0  0  1 
24         Canada 0  0  1 
25     Republic of Moldova 0  0  1 
26         Norway 0  0  1 
27         Serbia 0  0  1 
28        Slovakia 0  0  1 
29        Ukraine 0  0  1 
30        Uzbekistan 0  0  1 
+0

+1 regex कौशल के लिए। भले ही मैं इसे नियमित रूप से उपयोग करता हूं, फिर भी मैं इसके द्वारा बड़े पैमाने पर रहस्यमय हूं। –

+1

बेशक यह हमेशा एक अच्छा कौशल है: http://xkcd.com/208/ –