2016-07-31 125 views
2

我使用phantomJS作为硒的驱动程序。我的代码是用python编写的。我跟着从类似的问题的建议,并且现在用的是以下几点:使用代理phantomjs(硒webdriver)

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 

OUTPUT:

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

如果我只用通常的方式打电话给phantomJS,网站就像往常一样出现:

driver = webdriver.PhantomJS() 

仅供参考,我已经试过这与从这个名单一堆代理:

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

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

回答

4

我怀疑你使用的代理是不正确的。我尝试了以下使用代理在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被列入黑名单,谷歌提示的captcha框中看到的图像! IP已经改变了!

+0

nope - 我用你的代码,它在我的失败的地方工作。谢谢! – chris

+0

等不,这仍然会返回我的IP地址。你从来没有真正将代理传递给phantomjs驱动程序,你的意思是? – chris

+0

它看起来像代理变量用于Firefox的webdriver,而不是phantomjs – chris