gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:21 24 4
gpt4 key购买 nike

希望通过我的 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 代码可在使用 Python3 的 Windows 中运行:

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

最佳答案

像下面这样的东西应该可以工作:

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."

关于python - 在 Linux/Windows 中使用 RSelenium 的 Tor 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39048810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com