我在我的脚本中登录时遇到问题.尽管我在stackoverflow上找到了所有其他好的答案,但没有一个解决方案适合我.
我正在为我的博士研究抓一个网络论坛,它的URL是http://forum.axishistory.com.
我想要抓取的网页是成员列表 – 列出所有成员个人资料的链接的页面.如果登录,则只能访问成员列表.如果您尝试在不登录的情况下访问成员列表,则会显示登录表单.
成员列表的URL是:http://forum.axishistory.com/memberlist.php.
我试过httr-package:
library(httr) members <- GET("http://forum.axishistory.com/memberlist.PHP",authenticate("username","password")) members_html <- html(members)
然后我尝试了RCurl:
library(RCurl) members_html <- htmlParse(getURL("http://forum.axishistory.com/memberlist.PHP",userpwd = "username:password")) members_html
然后我尝试了这个主题的list()函数 – Scrape password-protected website in R:
handle <- handle("http://forum.axishistory.com/") path <- "ucp.PHP?mode=login" login <- list( amember_login = "username",amember_pass = "password",amember_redirect_url = "http://forum.axishistory.com/memberlist.PHP" ) response <- POST(handle = handle,path = path,body = login)
我正在研究的下一件事是RSelenium,但经过所有这些尝试,我试图弄清楚我是否可能遗漏了某些东西(可能是完全明显的东西).
我在这里查看了其他相关帖子,但无法弄清楚如何将代码应用于我的案例:
How to use R to download a zipped file from a SSL page that requires cookies
Scrape password-protected website in R
How to use R to download a zipped file from a SSL page that requires cookies
https://stackoverflow.com/questions/27485311/scrape-password-protected-https-website-in-r
解决方法
感谢西蒙,我在这里找到了答案:
Using rvest or httr to log in to non-standard forms on a webpage
library(rvest) url <-"http://forum.axishistory.com/memberlist.PHP" pgsession <-html_session(url) pgform <-html_form(pgsession)[[2]] filled_form <- set_values(pgform,"username" = "username","password" = "password") submit_form(pgsession,filled_form) memberlist <- jump_to(pgsession,"http://forum.axishistory.com/memberlist.PHP") page <- html(memberlist) usernames <- html_nodes(x = page,css = "#memberlist .username") data_usernames <- html_text(usernames,trim = TRUE)