使用 RSelenium 在 Chrome 中启用 Adblocker 扩展

mks*_*212 5 selenium google-chrome r rselenium

我正在从这个页面抓取 R 中的数据,http://finviz.com/screener.ashx? v= 111&f=earningsdate_nextdays5显示弹出广告。这些广告会干扰脚本,所以我想启用广告拦截器扩展:https ://chrome.google.com/webstore/detail/adblock/gighmmpiobklfepjocnamgkkbiglidom

我在这里使用 RSelenium 包文档中的代码,https: //cran.r-project.org/web/packages/RSelenium/RSelenium.pdf

我通过打开 chrome 浏览器并导航到:chrome://version/ 找到了配置文件目录。这是我常用的配置文件,它启用了广告拦截器扩展。

但是,当我打开 chrome 时,没有广告拦截器。我查看了这个页面,http: //scottcsims.com/wordpress/?p=450,他建议使用似乎没有在 RSelenium 中实现的 add_extension 方法。

关于如何在 R 打开的浏览器中启用广告拦截器的任何想法?

我的代码到目前为止。请注意,这是在 mac 上完成的,当然您的用户名将与我的不同,因此请务必将 getChromeProfile 中的第一个参数更改为您在此页面上的 Profile Path 中找到的内容,chrome://version/

require(RSelenium)
RSelenium::startServer()
cprof <- getChromeProfile("/Users/<username>/Library/Application Support/Google/Chrome/", "Profile 1")
remDr <<- remoteDriver(browserName = "chrome", extraCapabilities = cprof)
remDr$open()
appURL <- "http://finviz.com/screener.ashx?v=111&f=earningsdate_nextdays5"
remDr$navigate(appURL)
Run Code Online (Sandbox Code Playgroud)

Nea*_*sch 1

它不是一个块扩展,因为我更喜欢 adguard,但我总是使用它:

从 Chrome 以外的浏览器下载 adguard : https: //www.crx4chrome.com/go.php ?d=4687&i=158&p=31932&s=1&l=https%3A%2F%2Fclients2.googleusercontent.com%2Fcrx%2Fblobs%2FQwAAAHF3InbmK -wFIemaY3I3BCPa0e33dMYlYToYq-WCs1jSyPlSXnr3dNv-HTinVL8eTmtbBlPjwi-hJEL5_ZnPfXkYphLdiwB7LVwS3slKcj15AMZSmuWuPGYPZfS0woRX9brTIZ8faUYQCg%2Fextension_3_0_13_0.crx

下载文件路径示例:/Users/admin/Downloads/extension_3_0_13_0.crx

R代码:

library(RSelenium) #install_github("ropensci/RSelenium")
cprof <- list(chromeOptions = 
                list(extensions = 
                       list(base64enc::base64encode("/Users/admin/Downloads/extension_3_0_13_0.crx"))
                ))


rD <- rsDriver(port = 4444L,extraCapabilities=cprof, browser ="chrome",chromever = "latest"))

#if error port used or need to clear port
#rm(rD)
#rm(remDr)
#gc() #then try again

#set timeout preferences with chrome client
remDr <- rD$client
remDr$setTimeout(type = 'page load', milliseconds = 120000)
remDr$setTimeout(type = 'implicit', milliseconds = 120000)
Run Code Online (Sandbox Code Playgroud)