Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从selenium page_源获取内容类型_Python_Selenium_Phantomjs_Content Type - Fatal编程技术网

Python 如何从selenium page_源获取内容类型

Python 如何从selenium page_源获取内容类型,python,selenium,phantomjs,content-type,Python,Selenium,Phantomjs,Content Type,我知道内容类型可以从 response = urllib2.urlopen(url) content-type = response.info().getheader('Content-type') 现在,我需要执行js代码,所以我选择selenium和Phantomjs来获取网页 driver = webdriver.PhantomJS() driver.get(url) source = driver.page_source 如何在不下载两次网页的情况下从源代码获取内容类型?我知道我可以

我知道内容类型可以从

response = urllib2.urlopen(url)
content-type = response.info().getheader('Content-type')
现在,我需要执行js代码,所以我选择selenium和Phantomjs来获取网页

driver = webdriver.PhantomJS()
driver.get(url)
source = driver.page_source

如何在不下载两次网页的情况下从源代码获取内容类型?我知道我可以将response.read()保存为html文件,然后驱动程序呈现本地html文件,而无需再次下载。但是,它太慢了。有什么建议吗

Selenium不获取标题,但您可以通过以下请求请求标题:

import  requests

print(requests.head(url).headers["Content-Type"])

您可以使用httplib2、urliib2等。。有许多例子显示了如何使用各种LIB请求head

谢谢!这真的很有帮助。不用担心,head请求应该非常有效。