使用python从网页抓取标题

时间:2011-12-24 23:43:10

标签: python html http python-3.x

如何使用python 3访问HTTP标头。具体来说,我正在尝试重新创建您可以通过Chrome中的开发人员工具中的网络访问的标题。

1 个答案:

答案 0 :(得分:4)

>>> import pprint
>>> import urllib.request
>>> u = urllib.request.urlopen('http://www.python.org')
>>> pprint.pprint(dict(u.getheaders()))
{'Accept-Ranges': 'bytes',
 'Connection': 'close',
 'Content-Length': '18882',
 'Content-Type': 'text/html',
 'Date': 'Sat, 24 Dec 2011 23:51:27 GMT',
 'ETag': '"105800d-49c2-4b4ab1ba443c0"',
 'Last-Modified': 'Thu, 22 Dec 2011 09:41:43 GMT',
 'Server': 'Apache/2.2.16 (Debian)',
 'X-Pad': 'avoid browser bug'}
相关问题