【问题标题】:Using a proxy with phantomjs (selenium webdriver)使用带有 phantomjs 的代理(selenium webdriver)
【发布时间】:2016-12-05 11:09:57
【问题描述】:

我正在使用 phantomJS 作为 selenium 的驱动程序。我的代码是用python编写的。我遵循了类似问题的建议,并正在使用以下内容:

service_args = [
    '--proxy=78.23.244.145:80',
    '--proxy-type=http',
    ]
driver = webdriver.PhantomJS(service_args=service_args)
driver.get('http://www.whatismyip.com/')

但是,当我打印 html 时,几乎没有任何显示:

print driver.page_source

输出:

<html><head></head><body></body></html>

如果我只使用对 phantomJS 的通常调用来执行此操作,则该网站将照常显示:

driver = webdriver.PhantomJS()

作为参考,我已经用这个列表中的一堆代理尝试了这个:

http://proxylist.hidemyass.com/search-1291972#listable

我想知道如何在使用代理时让页面正确显示。任何帮助,将不胜感激!

【问题讨论】:

    标签: python selenium proxy phantomjs


    【解决方案1】:

    我怀疑您使用的代理不正确。我尝试了以下方法,其中使用的代理在 Windows 8 中表现良好。

    from selenium.webdriver.common.proxy import *
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    phantomjs_path = r"E:\Software & Tutorial\Phantom\phantomjs-2.1.1-windows\bin\phantomjs.exe"
    service_args = [
        '--proxy=217.156.252.118:8080',
        '--proxy-type=https',
        ]
    
    driver = webdriver.PhantomJS(executable_path=phantomjs_path,service_args=service_args)
    driver.get("https://www.google.com.bd/?gws_rd=ssl#q=what+is+my+ip")
    print driver.page_source.encode('utf-8')
    print "="*70
    print driver.title
    driver.save_screenshot(r"E:\Software & Tutorial\Phantom\test.png")
    driver.quit()
    

    查看保存的图像(test.png)并查看状态。如果使用的 ip 被列入黑名单,谷歌提示的验证码框会看到该图像! IP已更改!

    【讨论】:

    • 不——我使用了你的代码,它在我失败的地方工作。谢谢!
    • 等待不,这仍然返回我的 IP 地址。你从来没有真正将代理传递给 phantomjs 驱动程序,你打算这样做吗?
    • 代理变量好像是用firefox webdriver,而不是phantomjs
    • 更改服务参数对我有帮助。 service_args = ['--proxy=127.0.0.1:9050', '--proxy-type=socks5', ]
    • Proxy-type=https 是错误的,唯一的选项是“http/socks/none”,所以它导致了错误,只是没有使用代理。
    猜你喜欢
    • 2013-06-25
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多