Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Rselenium/PhantomJS的http身份验证_R_Selenium_Phantomjs_Rvest - Fatal编程技术网

使用Rselenium/PhantomJS的http身份验证

使用Rselenium/PhantomJS的http身份验证,r,selenium,phantomjs,rvest,R,Selenium,Phantomjs,Rvest,对于Rselenium来说,这是一个非常新的概念,使用Chrome进行调试,然后使用PhantomJS进行生产(因为我可以在循环中运行脚本,而不会弹出浏览器窗口) 我正试图抓取一个https网站,它有一个非常普通的身份验证弹出窗口。当我使用Chrome时,我可以使用这种格式。然而,当我使用phantomjs时,这似乎不起作用。使用RSelenium来驱动PhantomJS是否有一种很好的方法来导入凭据 如果没有,是否有更好的方法?具有讽刺意味的是,我可以使用rvest/httr登录该站点。。。问

对于Rselenium来说,这是一个非常新的概念,使用Chrome进行调试,然后使用PhantomJS进行生产(因为我可以在循环中运行脚本,而不会弹出浏览器窗口)

我正试图抓取一个https网站,它有一个非常普通的身份验证弹出窗口。当我使用Chrome时,我可以使用这种格式。然而,当我使用phantomjs时,这似乎不起作用。使用RSelenium来驱动PhantomJS是否有一种很好的方法来导入凭据

如果没有,是否有更好的方法?具有讽刺意味的是,我可以使用rvest/httr登录该站点。。。问题是它太重java了,我真的需要RSelenium来导航并最终获取我需要的数据

一些示例代码,但很遗憾,我无法提供我引用的受密码保护的站点:

library(RSelenium)
library(httr)
library(wdman)
selCommand<-wdman::selenium(jvmargs = c("-Dwebdriver.chrome.verboseLogging=true"),
                        retcommand = TRUE)
cat(selCommand)
#start Selenium server via shell script

remDr <- remoteDriver(port = 4567L, browserName = "chrome")
#remDr <- remoteDriver(port = 4567L, browserName = "phantomjs")
remDr$open()
remDr$navigate("https://user:pass@www.somewebiste.com") #works with chrome, 
                                                        #does not work with PhantomJS
库(RSelenium)
图书馆(httr)
图书馆(wdman)

selCommand您可以通过使用
getAllCookies
登录使用cookies。然后,在PhantomJS浏览器中,调用
addCookie

如果调用首先是
http
,而不是
https

library(RSelenium)

rD <- rsDriver(browser = "phantom")
remDr <- rD$client

remDr$navigate("http://user:passwd@httpbin.org/basic-auth/user/passwd")
> remDr$getPageSource()[[1]]
[1] "<html><head></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\n  \"authenticated\": true, \n  \"user\": \"user\"\n}\n</pre></body></html>"
rm(rD)
gc()
库(RSelenium)

不幸的是,它必须是https。同时,我连网址都找不到。。。i、 e即使我完全省略了user/pass,并尝试导航到此https站点,然后运行remDr$getCurrenturl(),我看到它仍然是大约:空白,即没有任何更改。尝试设置自定义标题,显然将
user:passwd
替换为实际值。如果这不起作用,那么需要特定的网站进一步调查这个问题。不幸的是,没有骰子。就我个人的理解而言,自定义头的想法是通过调用$navigate()有效地传递的吗?i、 您在上面编写的代码中的自定义头是否会应用于任何基本的http授权弹出窗口?再次感谢您的帮助。是的,自定义标题已添加到导航呼叫中。如果在给出的示例中省略自定义头,您将看到身份验证不会发生。
base64pw <- paste("Basic", 
                  base64enc::base64encode(charToRaw("user:passwd")))
eCaps <- list( "phantomjs.page.customHeaders.Authorization" = base64pw)
rD <- rsDriver(browser = "phantom", extraCapabilities = eCaps)
remDr <- rD$client

remDr$navigate("http://httpbin.org/basic-auth/user/passwd")
> remDr$getPageSource()[[1]]
[1] "<html><head></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\n  \"authenticated\": true, \n  \"user\": \"user\"\n}\n</pre></body></html>"
rm(rD)
gc()