urlretrieveだけで直接request headerの中身を見る方法はわかりませんでした。
あとurlretrieveは非推奨です。
import os
from urllib.request import HTTPHandler, urlretrieve, build_opener
from http.client import HTTPConnection
class MyHTTPConnection(HTTPConnection):
def send(self, s):
print("[[request_header:]]\n%s" % s.decode())
HTTPConnection.send(self, s)
class MyHTTPHandler(HTTPHandler):
def http_open(self, req):
return self.do_open(MyHTTPConnection, req)
def download_file(url):
dst_path=os.path.basename(url) + '.png'
opener = build_opener(MyHTTPHandler)
_ = opener.open(url)
_, mes = urlretrieve(url, dst_path)
print("[[response_header]]: \n%s" % mes)
download_file('http://www.example.com/')
参考:https://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib2-urlopen-call
追記
HTTPSの場合は下記。
import os,pprint
from urllib.request import HTTPSHandler, build_opener, urlretrieve
from http.client import HTTPSConnection
from urllib.request import Request
class MyHTTPConnection(HTTPSConnection):
def send(self, s):
print("[[request_header:]]\n%s" % s.decode())
HTTPSConnection.send(self, s)
class MyHTTPHandler(HTTPSHandler):
def https_open(self, req):
return self.do_open(MyHTTPConnection, req)
def download_file(url):
dst_path=os.path.basename(url)
opener = build_opener(MyHTTPHandler)
_ = opener.open(url)
_, mes = urlretrieve(url, dst_path)
print("[[response_header]]: \n%s" % mes)
download_file('https://www.example.com/')
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/22 02:37
2020/12/22 02:38
2020/12/22 03:15