我的脚本登录时遇到问题。尽管我在stackoverflow上找到了所有其他好的答案,但没有一个解决方案适合我。
我正在为我的博士研究抓一个网络论坛,其网址为http://forum.axishistory.com。
我想要抓取的网页是会员列表 - 列出所有会员资料的链接的页面。如果登录,则只能访问成员列表。如果您尝试在未登录的情况下访问成员列表,则会显示登录表单。
成员列表的网址为: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
答案 0 :(得分:8)
感谢西蒙,我在这里找到了答案: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)