情况
我有一个简单的python脚本来获取给定URL的HTML源:
browser = webdriver.PhantomJS()
browser.get(url)
content = browser.page_source
有时,该URL指向具有缓慢加载的外部资源(例如视频文件或非常慢的广告内容)的页面.
在完成.get(url)请求之前,Webdriver将一直等到加载这些资源.
注意:由于无关紧要的原因,我需要使用PhantomJS而不是请求或urllib2
这个问题
我想在PhantomJS资源加载上设置超时,这样如果资源加载时间过长,浏览器就会认为它不存在或者其他什么.
这将允许我根据浏览器加载的内容执行后续的.pagesource查询.
Documentation on webdriver.PhantomJS非常薄,我没有在SO上找到类似的问题.
提前致谢!
解决方法:
PhantomJS提供了resourceTimeout,它可能适合您的需求.我引用文档here
(in milli-secs) defines the timeout after which any resource requested
will stop trying and proceed with other parts of the page.
onResourceTimeout callback will be called on timeout.
所以在Ruby中,你可以做类似的事情
require 'selenium-webdriver'
capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs("phantomjs.page.settings.resourceTimeout" => "5000")
driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities
我相信Python,它就像(未经测试,只提供逻辑,你是Python开发人员,希望你会弄清楚)
driver = webdriver.PhantomJS(desired_capabilities={'phantomjs.page.settings.resourceTimeout': '5000'})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。