2014-11-28 2 views
4

한 번에 하나씩 개별 링크를 지정하지 않고 웹 페이지에서 모든 zip 파일을 다운로드 할 수 있습니까?웹 페이지에서 반복적으로 zip 파일 다운로드 (Windows)

모든 월간 계정 zip 파일을 http://download.companieshouse.gov.uk/en_monthlyaccountsdata.html에서 다운로드하고 싶습니다.

Windows 8.1, R3.1.1을 사용하고 있습니다. PC에는 wget이 없으므로 재귀 호출을 사용할 수 없습니다.

대체 : 나는 웹 페이지 텍스트 자체를 다운로드하려고했습니다. 그런 다음 각 zip 파일의 이름을 추출하여 루프에서 download.file으로 전달할 수 있습니다. 그러나, 나는 이름을 추출하는 데 어려움을 겪고있다.

pth <- "http://download.companieshouse.gov.uk/en_monthlyaccountsdata.html" 

temp <- tempfile() 
download.file(pth,temp) 
dat <- readLines(temp) 
unlink(temp) 

g <- dat[grepl("accounts_monthly", tolower(dat))] 

g에는 다른 문자들 중 파일 이름이있는 문자열이 포함되어 있습니다.

g 
[1] "     <li><a href=\"Accounts_Monthly_Data-September2013.zip\">Accounts_Monthly_Data-September2013.zip (775Mb)</a></li>" 
[2] "     <li><a href=\"Accounts_Monthly_Data-October2013.zip\">Accounts_Monthly_Data-October2013.zip (622Mb)</a></li>" 

내가 지금의 파일 Accounts_Monthly_Data-September2013.zip 그리고 이름을 추출하고 싶지만, 내 정규식 (직접보고)

gsub(".*\\>(\\w+\\.zip)\\s+", "\\1", g) 

데이터

g <- c("     <li><a href=\"Accounts_Monthly_Data-September2013.zip\">Accounts_Monthly_Data-September2013.zip (775Mb)</a></li>", 
"     <li><a href=\"Accounts_Monthly_Data-October2013.zip\">Accounts_Monthly_Data-October2013.zip (622Mb)</a></li>" 
) 
+1

좋은 질문입니다. 이해하기 쉽지 않습니다. –

답변

5

아주 끔찍한있는 사용 XML 패키지 :

pth <- "http://download.companieshouse.gov.uk/en_monthlyaccountsdata.html" 
library(XML) 
doc <- htmlParse(pth) 
myfiles <- doc["//a[contains(text(),'Accounts_Monthly_Data')]", fun = xmlAttrs] 
fileURLS <- file.path("http://download.companieshouse.gov.uk", myfiles) 
mapply(download.file, url = fileURLS, destfile = myfiles) 

"//a[contains(text(),'Accounts_Monthly_Data')]"XPATH 표현입니다. XML 패키지가 "Accounts_Monthly_Data"텍스트를 포함하는 앵커 (a) 인 모든 노드를 선택하도록 지시합니다. 이 결과는 노드 목록입니다. fun = xmlAttrs 인수가 xmlAttrs 기능이 노드를 통과하는 XML 패키지를 알려줍니다. 이 함수는 xml 노드에서 속성을 제거합니다. 앵커는이 경우에는 하나의 속성 (우리가 찾고있는 것) 인 href만을 가지고 있습니다.

+0

답변 해 주셔서 감사합니다. – user2957945

관련 문제