首页 > 解决方案 > 需要登录的R中的网络抓取

问题描述

我想登录我的帐户并下载并继续在网站上继续,但我使用 rvest 包的 r 代码不起作用。

link_of_login <- "https://www.fio.cz/e-broker/e-broker.cgi"
pgsession <- html_session(link_of_login)
pgform <- html_form(pgsession)[[1]]
filled_form <- set_values(pgform, LOGIN_USERNAME="USERNAME", LOGIN_PASSWORD="PASSWORD")
submit_form(pgsession, filled_form)

但是 R 正在返回:错误:form不包含action属性。

之后我想 jump_to("https://www.fio.cz/e-broker/e-obchody.cgi")

我想提交登录信息的 GET 和 POST 方法可能有问题。那么有什么简单的解决方案吗?非常感谢。

标签: rweb-scrapingrvest

解决方案


一种解决方案可能是RSelenium

下面是一个简单的例子

library(RSelenium)
#Selenium environment activation
rD <- RSelenium::rsDriver(browser = "firefox", check = FALSE)
remDr <- rD[["client"]]
remDr$navigate("https://www.fio.cz/e-broker/e-broker.cgi")
#Click to activate the field and send the username
remDr$findElement(using = 'xpath', value = '/html/body/div/form/div[1]/div[1]/div[5]/table/tbody/tr[1]/td[2]/input')$clickElement()
remDr$findElement(using = 'xpath', value = '/html/body/div/form/div[1]/div[1]/div[5]/table/tbody/tr[1]/td[2]/input')$sendKeysToElement(list('USERNAME'))
#Click to activate the field and send the password 
remDr$findElement(using = 'xpath', value = '/html/body/div/form/div[1]/div[1]/div[5]/table/tbody/tr[1]/td[2]/input')$clickElement()
remDr$findElement(using = 'xpath', value = '/html/body/div/form/div[1]/div[1]/div[5]/table/tbody/tr[2]/td[2]/input')$sendKeysToElement(list('PASSWORD'))
#Click the bottom
remDr$findElement(using = 'xpath', value = '/html/body/div/form/div[1]/div[1]/div[5]/table/tbody/tr[3]/td[2]/input')$clickElement()

推荐阅读