在Linux / Windows中使用RSelenium的Tor浏览器

时间:2016-08-19 23:08:22

标签: python r selenium tor rselenium

希望使用我的Linux机器使用RSelenium和Tor来返回Tor IP(使用Firefox作为Tor浏览器)。这对于Python是可行的,但在R中遇到问题。任何人都可以使用它吗?也许您可以在Windows / Linux中共享您的解决方案。

# library(devtools)
# devtools::install_github("ropensci/RSelenium")
library(RSelenium)

RSelenium::checkForServer()
RSelenium::startServer() 

binaryExtension <- paste0(Sys.getenv('HOME'),"/Desktop/tor-browser_en-US/Browser/firefox")
remDr <- remoteDriver(dir = binaryExtention)

remDr$open()
remDr$navigate("http://myexternalip.com/raw")
remDr$quit()

正在返回错误Error in callSuper(...) : object 'binaryExtention' not found

对于社区参考,此Selenium代码在Windows中使用Python3:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

from os.path import expanduser # Finds user's user name on Windows

# Substring inserted to overcome r requirement in FirefoxBinary 
binary = FirefoxBinary(r"%s\\Desktop\\Tor Browser\\Browser\\firefox.exe"  % (expanduser("~")))
profile = FirefoxProfile(r"%s\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default" % (expanduser("~")))

driver = webdriver.Firefox(profile, binary)
driver.get('http://myexternalip.com/raw')   
html = driver.page_source
soup = BeautifulSoup(html, "lxml") # lxml needed

# driver.close()

# line.strip('\n')
"Current Tor IP: " + soup.text.strip('\n')

# Based in part on
# http://stackoverflow.com/questions/13960326/how-can-i-parse-a-website-using-selenium-and-beautifulsoup-in-python
# http://stackoverflow.com/questions/34316878/python-selenium-binding-with-tor-browser
# http://stackoverflow.com/questions/3367288/insert-variable-values-into-a-string-in-python

3 个答案:

答案 0 :(得分:5)

以下内容应该有效:

browserP <- paste0(Sys.getenv('HOME'),"/Desktop/tor-browser_en-US/Browser/firefox")
jArg <- paste0("-Dwebdriver.firefox.bin='", browserP, "'")
selServ <- RSelenium::startServer(javaargs = jArg)

更新:

这在Windows上对我有用。首先运行测试版:

checkForServer(update = TRUE, beta = TRUE, rename = FALSE)

接下来手动打开一个版本的tor浏览器。

library(RSelenium)
browserP <- "C:/Users/john/Desktop/Tor Browser/Browser/firefox.exe"
jArg <- paste0("-Dwebdriver.firefox.bin=\"", browserP, "\"")
pLoc <- "C:/Users/john/Desktop/Tor Browser/Browser/TorBrowser/Data/Browser/profile.meek-http-helper/"
jArg <- c(jArg, paste0("-Dwebdriver.firefox.profile=\"", pLoc, "\""))
selServ <- RSelenium::startServer(javaargs = jArg)

remDr <- remoteDriver(extraCapabilities = list(marionette = TRUE))
remDr$open()
remDr$navigate("https://check.torproject.org/")

> remDr$getTitle()
[[1]]
[1] "Congratulations. This browser is configured to use Tor."

答案 1 :(得分:3)

适用于MacOS Sierra。

首先,您需要配置Firefox和Tor浏览器手动代理。

转到偏好设置&gt;高级&gt;网络&gt;设置

设置SOCKS主机:127.0.0.1端口:9150检查 - &gt;在浏览器菜单栏中的SOCKS v5上。

在Rstudio中运行R脚本时,您还需要打开Tor浏览器....否则您将在firefox浏览器中收到消息&#34;代理服务器拒绝连接&#34;

您还需要在脚本个人资料名称

中复制您的firefox个人资料的名称

打开Finder并转到/ Users / 用户名 /图书馆/应用支持/ Firefox /个人资料/ 个人资料名称

我的R测试脚本

 require(RSelenium)

    fprof <- getFirefoxProfile("/Users/**username**/Library/Application\ Support/Firefox/Profiles/nfqudbv2.default-1484451212373",useBase=TRUE)

    remDrv <- remoteDriver( browserName = "firefox"
                            , extraCapabilities = fprof)

    remDrv$open()
    remDrv$navigate("https://check.torproject.org/")

这将打开带有消息的Firefox浏览器实例 &#34;恭喜。此浏览器配置为使用Tor。&#34;

答案 2 :(得分:1)

警告:我没有进行过广泛的测试,但似乎有效。

依赖@ Ashley72的一些想法,但避免手动设置和复制(以及现在来自@jdharrison的解决方案所需的Rselenium功能)以及来自https://indranilgayen.wordpress.com/2016/10/24/make-rselenium-work-with-r/的一些想法调整以下配置文件选项(我通常调整一些其他选项,但它们似乎与问题无关):

fprof <- makeFirefoxProfile(list(network.proxy.socks = "127.0.0.1", # for proxy settings specify the proxy host IP  
network.proxy.socks_port = 9150L, # proxy port. Last character "L" for specifying integer is very important and if not specified it will not have any impact
network.proxy.type = 1L, # 1 for manual and 2 for automatic configuration script. here also "L" is important    
network.proxy.socks_version=5L, #ditto     
network.proxy.socks_remote_dns=TRUE))

然后像往常一样启动服务器:

rD <- rsDriver(port = 4445L, browser = "firefox", version = "latest", geckover = "latest", iedrver = NULL, phantomver = "2.1.1",
               verbose = TRUE, check = TRUE, extraCapabilities = fprof) # works for selenium server: 3.3.1 and geckover: 0.15.0; Firefox: 52
remDr <- rD[["client"]]
remDr <- rD$client
remDr$navigate("https://check.torproject.org/") # should confirm tor is setup
remDr$navigate("http://whatismyip.org/") # should confirm tor is setup

如您所见,我没有对牵线木偶选项进行更改。我不知道其含义是什么。请评论。

编辑:Tor浏览器必须启动并运行。否则,Rselenium打开的浏览器会出现错误&#34;代理服务器拒绝连接。&#34;