简体   繁体   English

使用Python从单个请求获取HTML和标头

[英]Get html and headers from single request using Python

I'm investigating the possibility of making a single http request using python to retrieve both the html as well as http headers info instead of having to make 2 seperate calls. 我正在研究使用python发出单个http请求以检索html和http标头信息的可能性,而不必进行2个单独的调用。

Anyone know of any good ways? 有人知道什么好方法吗?

Also what is the performance differences between the different methods of making these requests, eg urllib2 and httpconnection, etc. 另外,发出这些请求的不同方法(例如urllib2和httpconnection等)之间的性能差异是什么?

Just use urllib2.urlopen() . 只需使用urllib2.urlopen() The HTML can be retrieved by calling the read() method of the returned object, and the headers are available in the headers attribute. 可以通过调用返回对象的read()方法来检索HTML,并且标头在headers属性中可用。

import urllib2
f = urllib2.urlopen('http://www.google.com')

>>> print f.headers
Date: Fri, 08 Jun 2012 12:57:25 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Connection: close

>>> print f.read()
<!doctype html><html itemscope itemtype="http://schema.org/WebPage"><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
... etc ...

如果使用HTTPResponse ,则可以通过两个函数调用头和内容,但是不会两次访问服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python请求库从单个GET请求中获取标头和内容 - Obtain both headers and content from single GET request with the Python requests library 如何使用python发送包含标头的GET请求 - How to send GET request including headers using python 使用python下的keep-alive连接的带有头的GET请求 - GET request with headers using keep-alive connection under python Python request.get 一个单词的响应,我尝试了不同的标题 - Python request.get response with a single word and i tried different headers 如何使用Python请求获取请求标头而不是响应标头 - How to get request headers rather than response headers using Python Requests 使用Google Finance的python在一次请求中获取多只股票的历史数据 - Get historical data for multiple stocks in single request using python from Google finance 在使用 axios 发送请求时,无法在 http 拦截器 python fastapi 中获取请求标头 - Not able to get request headers in http interceptors python fastapi while sending request using axios 如何通过python发送带有标头的GET请求 - How to send a GET request with headers via python 获取响应标头发布请求 python - get response headers post request python 如何在 python scrapy 中获取请求标头 - How to get request headers in python scrapy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM