2013-05-30 21 views
5

Próbuję wykonać zadanie za pomocą R do skrobania danych na stronie internetowej.Skrobanie z witryny aspx za pomocą R

  1. Chciałbym przejść przez każdego linku na następnej stronie: http://capitol.hawaii.gov/advreports/advreport.aspx?year=2013&report=deadline&rpt_type=&measuretype=hb&title=House Bills

  2. wybrać tylko te elementy z Obecny status pokazano „przekazywane do gubernatora”. Na przykład: http://capitol.hawaii.gov/measure_indiv.aspx?billtype=HB&billnumber=17&year=2013

  3. A następnie złomowanie komórek w ramach STATUS TEXT dla następującej klauzuli "Passsed Final Reading". Na przykład: zaliczone czytanie końcowe poprawione w SD 2 z przedstawicielem Fale, Jordan, Tsuji głosujące tak, z zastrzeżeniami; Przedstawiciel (y) Cabanilla, Morikawa, Oshiro, Tokioka głosujący nie (4) i nikt nie przeprosił (0).

Próbowałem używać poprzednich przykładów z pakietami Rcurl i XML (w R), ale nie wiem, jak używać ich poprawnie dla stron aspx. Więc chciałbym mieć to: 1. Kilka sugestii, jak zbudować taki kod. 2. I zalecenie, jak nauczyć się wiedzy potrzebnej do wykonania takiego zadania.

Dzięki za wszelką pomoc,

Tom

+0

którą proponujemy przejrzeć tego wątku tutaj, gdzie starałem się dowiedzieć do skrobania strony internetowej. http://www.talkstats.com/showthread.php/26153- Trill-trying-to-learn-to-scrape?highlight=still+learning+to+scrape –

+0

Spędziłem kilka godzin na tym, to nie jest łatwe: (możesz pobrać zawartość pierwszej strony, ale druga nie akceptuje przejścia w "__VIEWSTATE" i kilku innych parametrów [jak pokazano tutaj] (http://stackoverflow.com/questions/15853204/how- do-logowania-a-następnie-pobrania-pliku-z-aspx-web-strony-z-r.) mogę dostać się do 'resp <-GET (" http://capitol.hawaii.gov/advreports/ advreport.aspx? year = 2013 & report = deadline & rpt_type = & measuretype = hb & title = Bony House "); writeBin (content (resp, 'raw'), tf); readHTMLTable (tf) $ GridViewReports', ale druga strona go zabija :( –

Odpowiedz

5
require(httr) 
require(XML) 

basePage <- "http://capitol.hawaii.gov" 

h <- handle(basePage) 

GET(handle = h) 

res <- GET(handle = h, path = "/advreports/advreport.aspx?year=2013&report=deadline&rpt_type=&measuretype=hb&title=House") 

# parse content for "Transmitted to Governor" text 
resXML <- htmlParse(content(res, as = "text")) 
resTable <- getNodeSet(resXML, '//*/table[@id ="GridViewReports"]/tr/td[3]') 
appRows <-sapply(resTable, xmlValue) 
include <- grepl("Transmitted to Governor", appRows) 
resUrls <- xpathSApply(resXML, '//*/table[@id ="GridViewReports"]/tr/td[2]//@href') 

appUrls <- resUrls[include] 

# look at just the first 

res <- GET(handle = h, path = appUrls[1]) 

resXML <- htmlParse(content(res, as = "text")) 


xpathSApply(resXML, '//*[text()[contains(.,"Passed Final Reading")]]', xmlValue) 

[1] "Passed Final Reading as amended in SD 2 with Representative(s) Fale, Jordan, 
Tsuji voting aye with reservations; Representative(s) Cabanilla, Morikawa, Oshiro, 
Tokioka voting no (4) and none excused (0)." 

pakiet Let httr obsługiwać wszystkie prace tło przez utworzenie handle.

Jeśli chcesz przejechać wszystkie 92 linków:

# get all the links returned as a list (will take sometime) 
# print statement included for sanity 
res <- lapply(appUrls, function(x){print(sprintf("Got url no. %d",which(appUrls%in%x))); 
            GET(handle = h, path = x)}) 
resXML <- lapply(res, function(x){htmlParse(content(x, as = "text"))}) 
appString <- sapply(resXML, function(x){ 
        xpathSApply(x, '//*[text()[contains(.,"Passed Final Reading")]]', xmlValue) 
         }) 


head(appString) 

> head(appString) 
$href 
[1] "Passed Final Reading as amended in SD 2 with Representative(s) Fale, Jordan, Tsuji voting aye with reservations; Representative(s) Cabanilla, Morikawa, Oshiro, Tokioka voting no (4) and none excused (0)." 

$href 
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."             
[2] "Passed Final Reading as amended in CD 1 with Representative(s) Cullen, Har voting aye with reservations; Representative(s) McDermott voting no (1) and none excused (0)." 

$href 
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."         
[2] "Passed Final Reading as amended in CD 1 with none voting aye with reservations; Representative(s) Hashem, McDermott voting no (2) and none excused (0)." 

$href 
[1] "Passed Final Reading, as amended (CD 1). 24 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 1 Excused: Ige."      
[2] "Passed Final Reading as amended in CD 1 with none voting aye with reservations; none voting no (0) and Representative(s) Say excused (1)." 

$href 
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."       
[2] "Passed Final Reading as amended in CD 1 with Representative(s) Johanson voting aye with reservations; none voting no (0) and none excused (0)." 

$href 
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none." 
[2] "Passed Final Reading as amended in CD 1 with none voting aye with reservations; none voting no (0) and none excused (0)." 
+0

Ty, user1609452.To jest dobry punkt wyjścia dla mnie, aby zrozumieć, jak zeskrobać strony aspx – user2300643

+0

dzięki od mnie też to jest fantastyczne :) –

+0

Przepraszamy, użytkownik1609452.Może być lista wszystkich odpowiednich adresów URL szczurów jej niż 1 na raz? Dzięki jeszcze raz! – user2300643