Web Scraping Capital Bikeshare R 中的个人旅行历史数据

Web Scraping Capital Bikeshare personal trip history data in R

我正在尝试在 Capital Bikeshare 网站上抓取我的旅行历史数据。我必须登录并转到行程菜单才能查看数据。但我得到这个错误:

> `No encoding supplied: defaulting to UTF-8.
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘readHTMLTable’ for signature ‘"xml_document"’

这是我的代码。

> library(httr)
> library(XML)
> handle <- handle("https://www.capitalbikeshare.com/")
> path <-"profile/trips"

> login <- list( profile_login="myemail", profile_pass ="mypassword", profile_redirect_url="https://secure.capitalbikeshare.com/profile/trips/QNURCMF2Q6")
> response <- POST(handle = handle, path = path, body = login)
> readHTMLTable(content(response))

我也尝试过使用 ,但后来我一直收到“Error: Unknown field names: _username, _password”错误。我应该在这里使用哪个字段? Id, name 等都试过了,还是不行。

首先,会员登录页面与上面列出的介绍页面不同:

这可能不正确,但请尝试将其作为可能的投资起点:

login<-"https://secure.capitalbikeshare.com/profile/login"

library(rvest)
pgsession<-html_session(login)
pgform<-html_form(pgsession)[[1]]
#update user id and password in the next line
filled_form<-set_values(pgform, "_username"="myemail@gmail.com", "_password"="password")
submit_form(pgsession, filled_form)

登录后可以使用 jump_to 功能移动到所需页面:

page<-jump_to(pgsession, newurl) #newurl will be the address where to go to next.

希望这有帮助,如果这不起作用,请发表评论,我会删除 post。