【问题标题】:Can't access website with Selenium-wire after using Pyinstaller使用 Pyinstaller 后无法使用 Selenium-wire 访问网站
【发布时间】:2020-12-03 11:50:17
【问题描述】:

我需要访问一个网站。为此,我需要使用具有身份验证和特定用户代理的代理。代码如下:

def start_driver(proxy_data, user_agent):
    proxy = (
            proxy_data.get('login') + ':' + proxy_data.get('password') +
            '@' + proxy_data.get('ip') + ':' + proxy_data.get('port')
    )
    executable_path = os.path.abspath(r'assets\geckodriver\driver.exe')
    firefox_binary = os.path.abspath(r'assets\firefox\browser.exe')
    firefox_options = Options()
    capabilities = webdriver.DesiredCapabilities().FIREFOX
    firefox_profile = FirefoxProfile()
    # firefox_options.add_argument('--headless')
    capabilities['pageLoadStrategy'] = 'eager'
    options = {
        'proxy': {
            'http': 'http://' + proxy,
            'https': 'https://' + proxy,
        }
    }
    driver = webdriver.Firefox(
        executable_path=executable_path,
        firefox_binary=firefox_binary,
        seleniumwire_options=options,
        capabilities=capabilities,
        firefox_profile=firefox_profile,
        firefox_options=firefox_options
    )
    driver.header_overrides = {'User-Agent': user_agent}
    return driver

为确保一切正常,我 ping http://whatsmyuseragent.org/

driver.get('http://whatsmyuseragent.org/')

这部分代码运行良好。 但是,当我获得目标网站时:

driver.get('https://domain.tld/')

我收到一个错误:

selenium.common.exceptions.WebDriverException:消息:到达错误 页面:关于:neterror?e=nssFailure2&u=...

有趣的是,当我直接通过 PyCharm 运行脚本时 - 一切正常。但是在使用pyinstaller 和以下参数后:

pyinstaller --onefile MyScript.py

Selenium 确实可以访问 http://whatsmyuseragent.org/,但无法访问目标网站 https://domain.tld

我确实认为问题出在pyinstaller 本身,但我真的不明白为什么会这样。我只有两个版本来解释为什么会发生:

  1. 使用pyinstaller 编译时出现问题。
  2. 目标网站不知何故不允许 Selenium 访问它(但为什么 Selenium 在使用 PyCharm 而不是 .exe 文件时可以访问它?)

pyinstaller有任何想法和/或替换吗?

【问题讨论】:

  • 你试过手动去https://domain.tld/吗?
  • @Jortega 是的,一切正常。而且,正如我所提到的,直接通过 PyCharm 运行脚本时一切正常。
  • 是的,我也面临同样的问题,它不适用于 exe

标签: python selenium geckodriver seleniumwire


【解决方案1】:

如果其他人遇到此问题,我找到了解决方案。 转到你的 python 文件夹,找到 pyinstaller hooks 文件夹,对我来说是:

https://github.com/wkeeling/selenium-wire/issues/84#issuecomment-624389859

C:\Python37-32\Lib\site-packages\PyInstaller\hooks

创建一个名为:

的新文件

hook-seleniumwire.py

里面你需要:

from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files('seleniumwire')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2020-10-17
    • 2021-04-28
    • 2017-03-13
    • 2022-12-14
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多