Python: 使用Selenium和PhantomJS页面源为空

20 投票
4 回答
13262 浏览
提问于 2025-04-18 06:06

我在Windows 7上使用Selenium和PhantomJS时遇到了一些麻烦,想要获取某个网址的页面源代码。使用browser.page_source时,它只返回了<html><head></head></html>,也就是一个空的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

在Linux上,使用相同版本的PhantomJS时,一切都运行得很好。而且在Windows Server 2003上也能正常工作。

4 个回答

0

下面的方法让我成功地增大了屏幕的大小:

driver = webdriver.PhantomJS(path2phantom, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 
driver.set_window_size(2000, 1500)

2
driverPhantom = webdriver.PhantomJS(driverLocation, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])      # initaling web driver for PhantomJs

对我来说有效。

8

使用 service_args=['--ignore-ssl-errors=true'] 解决了问题!

browser = webdriver.PhantomJS('phantomjs-1.9.7-windows\phantomjs.exe', service_args=['--ignore-ssl-errors=true'])
35

默认情况下,phantomjs使用的是SSLv3协议,但很多网站在发现SSL的漏洞后,已经转向使用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'])

撰写回答