用selenium打开浏览器

use*_*403 25 python selenium tor

是否有可能使硒使用TOR浏览器?有没有人有任何可以复制粘贴的代码?

use*_*679 12

不要使用TBB,只需在您使用的任何浏览器中设置正确的代理设置即可.以FF为例,像这样:

#set some privacy settings
ff_prof.set_preference( "places.history.enabled", False )
ff_prof.set_preference( "privacy.clearOnShutdown.offlineApps", True )
ff_prof.set_preference( "privacy.clearOnShutdown.passwords", True )
ff_prof.set_preference( "privacy.clearOnShutdown.siteSettings", True )
ff_prof.set_preference( "privacy.sanitize.sanitizeOnShutdown", True )
ff_prof.set_preference( "signon.rememberSignons", False )
ff_prof.set_preference( "network.cookie.lifetimePolicy", 2 )
ff_prof.set_preference( "network.dns.disablePrefetch", True )
ff_prof.set_preference( "network.http.sendRefererHeader", 0 )

#set socks proxy
ff_prof.set_preference( "network.proxy.type", 1 )
ff_prof.set_preference( "network.proxy.socks_version", 5 )
ff_prof.set_preference( "network.proxy.socks", '127.0.0.1' )
ff_prof.set_preference( "network.proxy.socks_port", 9050 )
ff_prof.set_preference( "network.proxy.socks_remote_dns", True )

#if you're really hardcore about your security
#js can be used to reveal your true i.p.
ff_prof.set_preference( "javascript.enabled", False )

#get a huge speed increase by not downloading images
ff_prof.set_preference( "permissions.default.image", 2 )

##
# programmatically start tor (in windows environment)
##
tor_path = "C:\\this\\is\\the\\location\\of\\" #tor.exe
torrc_path = "C:\\you\\need\\to\\create\\this\\file\\torrc"

DETACHED_PROCESS = 0x00000008
#calling as a detached_process means the program will not die with your python program - you will need to manually kill it
##
# somebody please let me know if there's a way to make this a child process that automatically dies (in windows)
##
tor_process = subprocess.Popen( '"' + tor_path+'tor.exe" --nt-service "-f" "' + torrc_path + '"', creationflags=DETACHED_PROCESS )

#attach to tor controller
## imports ##
# import stem.socket
# import stem.connection
# import stem.Signal
##
tor_controller = stem.socket.ControlPort( port=9051 )

control_password = 'password'
#in your torrc, you need to store the hashed version of 'password' which you can get with: subprocess.call( '"' + tor_path+'tor.exe" --hash-password %s' %control_password )

stem.connection.authenticate( tor_controller, password=control_password )

#check that everything is good with your tor_process by checking bootstrap status
tor_controller.send( 'GETINFO status/bootstrap-phase' )
response = worker.tor_controller.recv()
response = response.content()

#I will leave handling of response status to you
Run Code Online (Sandbox Code Playgroud)

  • 那么Tor的意义何在?那时您可以使用任何更快的匿名代理。使用整个TBB很重要。 (3认同)

DMf*_*fll 8

是的,可以使硒使用TOR浏览器.

我能够在Ubuntu和Mac OS X上都这样做.

必须要做两件事:

  1. 将二进制路径设置为Tor使用的firefox二进制文件.在Mac上,这条路径通常是/Applications/TorBrowser.app/Contents/MacOS/firefox.在我的Ubuntu机器上/usr/bin/tor-browser/Browser/firefox.

  2. Tor浏览器通过Vidalia或Tor安装在127.0.0.1:9150使用SOCKS主机.从Finder启动Tor一次并将其打开,以便Vidalia运行.使用selenium启动的实例也将使用Vidalia启动的SOCKS主机.

这是完成这两件事的代码.我在Mac OS X Yosemite上运行:

import os
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver


# path to the firefox binary inside the Tor package
binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'
if os.path.exists(binary) is False:
    raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)


browser = None
def get_browser(binary=None):
    global browser  
    # only one instance of a browser opens, remove global for multiple instances
    if not browser: 
        browser = webdriver.Firefox(firefox_binary=binary)
    return browser

if __name__ == "__main__":
    browser = get_browser(binary=firefox_binary)
    urls = (
        ('tor browser check', 'https://check.torproject.org/'),
        ('ip checker', 'http://icanhazip.com')
    )
    for url_name, url in urls:
        print "getting", url_name, "at", url
        browser.get(url)
Run Code Online (Sandbox Code Playgroud)

在Ubuntu系统上,我能够通过selenium运行Tor浏览器.这台机器在端口9051运行tor和在端口8118使用tor的privoxy http代理.为了让Tor浏览器通过tor检查页面,我必须将http代理设置为privoxy.

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver
browser = None

proxy_address = "127.0.0.1:8118"
proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': proxy_address,
})

tor = '/usr/bin/tor-browser/Browser/firefox'
firefox_binary = FirefoxBinary(tor)

urls = (
    ('tor_browser_check', 'https://check.torproject.org/'),
    ('icanhazip', 'http://icanhazip.com'),
)
keys, _ = zip(*urls)
urls_map = dict(urls)

def get_browser(binary=None, proxy=None):
    global browser
    if not browser:
        browser = webdriver.Firefox(firefox_binary=binary, proxy=proxy)
    return browser

if __name__ == "__main__":
    browser = get_browser(binary=firefox_binary, proxy=proxy)
    for resource in keys:
        browser.get(urls_map.get(resource))
Run Code Online (Sandbox Code Playgroud)

  • 在 Mac 上,对我不起作用:`selenium.common.exceptions.WebDriverException:消息:'geckodriver' 可执行文件需要在 PATH` 我将 firefox_binary 指向 Tor Browser.app 中的包,这显然不在我的脚本文件夹中. 如果有帮助,也可以使用 venv。任何人都能够在 Mac 上启动它? (2认同)

小智 6

//只需检查您的托管浏览器的端口号,并在//代码中相应地更改

from selenium import webdriver
profile=webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9150)
browser=webdriver.Firefox(profile)
browser.get("http://yahoo.com")
browser.save_screenshot("screenshot.png")
browser.close()
Run Code Online (Sandbox Code Playgroud)


Deb*_*anB 5

要使用Selenium驱动的GeckoDriver打开浏览器,您需要:

  • 下载并安装TOR 浏览器

  • 下载最新的GeckoDriver v0.26.0并将其放入您的系统中。

  • 安装最新的Mozilla Firefox v77.0.1浏览器。

  • 您可以使用以下代码块打开启用TOR 的浏览器:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import os
    
    torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9050)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    firefox_options = webdriver.FirefoxOptions()
    firefox_options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
    driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
    driver.get("http://check.torproject.org")
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

torFirefox_torproject


使用 Firefox Nightly 的替代方法

作为替代方案,您还可以下载、安装和使用最新的Firefox Nightly v79.0a1浏览器。

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    import os
    
    torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    profile = FirefoxProfile(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default')
    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.socks', '127.0.0.1')
    profile.set_preference('network.proxy.socks_port', 9050)
    profile.set_preference("network.proxy.socks_remote_dns", False)
    profile.update_preferences()
    firefox_options = webdriver.FirefoxOptions()
    firefox_options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
    driver = webdriver.Firefox(firefox_profile= profile, options = firefox_options, executable_path=r'C:\WebDrivers\geckodriver.exe')
    driver.get("http://check.torproject.org")
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

torNightly_torproject


使用 Chrome 的替代方法

作为替代方案,您还可以下载、安装和使用最新的Chrome v84浏览器。

  • 代码块:

    from selenium import webdriver
    import os
    
    torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
    PROXY = "socks5://localhost:9050" # IP:PORT or HOST:PORT
    options = webdriver.ChromeOptions()
    options.add_argument('--proxy-server=%s' % PROXY)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("http://check.torproject.org")
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

torChrome_torproject


参考

您可以在以下位置找到一些相关的详细讨论:


rds*_*oze 0

使用红宝石,

profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :socks => '127.0.0.1:9050' #port where TOR runs
browser = Watir::Browser.new :firefox, :profile => profile
Run Code Online (Sandbox Code Playgroud)

要确认您正在使用 Tor,请使用https://check.torproject.org/