如何在R中使用POST从web请求数据

How to use POST to request data from web in R

本文关键字:POST web 请求 数据      更新时间:2023-09-26

我正在学习R从一些网站获取数据。在这里,我试图使用R请求波士顿马拉松比赛结果从这个网站。

下面是我的代码:
library(httr)
login <- list(RaceYearLowID = "2016", LastName = "Desisa", 
              FirstName = "Lelisa", submit_button = "Search")
url <- "http://registration.baa.org/cfm_Archive/iframe_ArchiveSearch.cfm?mode=results&RequestTimeout=600&snap=47418326&"
res <- POST(url, body = login, verbose())

我得到的结果是:

-> POST /cfm_Archive/iframe_ArchiveSearch.cfm?mode=results&RequestTimeout=600&snap=47418326&
HTTP/1.1
-> Host: registration.baa.org
-> User-Agent: libcurl/7.50.3 r-curl/2.1 httr/1.2.1
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 467
-> Expect: 100-continue
-> Content-Type: multipart/form-data; boundary=------------------------47d84f94847f4213
->  <- HTTP/1.1 100 Continue
>> --------------------------47d84f94847f4213
>> Content-Disposition: form-data; name="RaceYearLowID"
>> 
>> 2016
>> --------------------------47d84f94847f4213
>> Content-Disposition: form-data; name="LastName"
>> 
>> Desisa
>> --------------------------47d84f94847f4213
>> Content-Disposition: form-data; name="FirstName"
>> 
>> Lelisa
>> --------------------------47d84f94847f4213
>> Content-Disposition: form-data; name="submit_button"
>> 
>> Search
>> --------------------------47d84f94847f4213--
<- HTTP/1.1 200 OK <- Date: Tue, 18 Oct 2016 17:47:31 GMT <- Server:
Microsoft-IIS/6.0 <- X-Powered-By: ASP.NET <- Set-Cookie:
CFID=7150121;expires=Thu, 11-Oct-2046 17:47:31 GMT;path=/ <-
Set-Cookie: CFTOKEN=95945596;expires=Thu, 11-Oct-2046 17:47:31
GMT;path=/ <- Set-Cookie: BAAARCHIVE_ISON=Testing;path=/ <-
Set-Cookie: CFID=7150121;path=/ <- Set-Cookie: CFTOKEN=95945596;path=/
<- Set-Cookie: BIGipServerBAA_registrationpool=304677130.20480.0000;
path=/ <- Transfer-Encoding: chunked <- Content-Type: text/html;
charset=UTF-8 <- Via: 1.1 dc1wsa01.na.coherentinc.com:80
(Cisco-WSA/9.1.1-074) <- Connection: close <- 
>

我查看了BAA网站的源代码,它是基于JavaScript函数的。任何建议吗?谢谢!

谢谢大家。我知道怎么得到结果了。代码如下:

login <- list(RaceYearLowID = "2014", LastName = "Smithson")url<——"http://registration.baa.org/cfm_Archive/iframe_ArchiveSearch.cfm?mode=results& RequestTimeout = 600,提前= 47418326,"

res <- POST(url, body = login, verbose(),encode = "form")

doc2<-htmlTreeParse(res,encoding = "utf-8",asText = TRUE)

doc2被解析为html样式。下一步是如何从这个html文件中获取数据。