Python:Selenium与PhantomJS空页源

Pau*_* R. 19 python selenium phantomjs

当我想获取URL页面的来源时,我在Windows7上遇到了Selenium和PhantomJS的问题. browser.page_source仅返回<html><head></head></html>.我以前睡过了,browser.page_source但没有用.

这是我的代码:

from selenium import webdriver
browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe')
url = 'myurl'
browser.get(url)
print browser.page_source
Run Code Online (Sandbox Code Playgroud)

在具有相同版本的PhantomJS的Linux上,它可以很好地工作.它也适用于Windows Server 2003.

小智 33

默认情况下,phantomjs使用SSLv3,但ssl中的bug之后的许多站点都迁移到tls.这就是你有空白页面的原因.使用service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
Run Code Online (Sandbox Code Playgroud)


Pau*_* R. 8

使用service_args=['--ignore-ssl-errors=true']了诀窍!

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true'])
Run Code Online (Sandbox Code Playgroud)