如何使用 python 找到网站的原始 header?

How to find the raw header of a website using python?

我正在自学网站。我正在尝试从网站获取原始 header 后跟 3 位 HTTP return 代码。 这是我到目前为止所做的:

import urllib.request
with urllib.request.urlopen('https://www.youtube.com/results?search_query=clippers+vs+lakers') as response:
    html_text = response.read()
print(html_text)

它从源头打印所有内容。然后我使用 "Command + F" 搜索一些关键字,如 "raw header",但我找不到有用的东西。 有人可以帮我从页面源中获取原始 header 吗?有没有图书馆可以做到这一点? 谢谢!

尝试response.info()方法获取headers。

最简单优雅的方法是使用请求库

import requests as req

url = 'https://www.youtube.com/results?search_query=clippers+vs+lakers'

response = req.get(url)

headers = response.headers

html = response.text