回答編集履歴
6
f
test
CHANGED
@@ -118,6 +118,6 @@
|
|
118
118
|
|
119
119
|
|
120
120
|
|
121
|
-
download_file(
|
121
|
+
download_file('https://www.example.com/')
|
122
122
|
|
123
123
|
```
|
5
f
test
CHANGED
@@ -121,7 +121,3 @@
|
|
121
121
|
download_file("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Hummingbird.jpg/800px-Hummingbird.jpg")
|
122
122
|
|
123
123
|
```
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
大体のことは公式ドキュメント(https://docs.python.org/ja/3/library/urllib.request.html、 https://docs.python.org/ja/3/library/http.client.html)に書いてあります。
|
4
修正
test
CHANGED
@@ -76,13 +76,11 @@
|
|
76
76
|
|
77
77
|
from urllib.request import HTTPSHandler, build_opener, urlretrieve
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
79
|
from http.client import HTTPSConnection
|
84
80
|
|
85
81
|
from urllib.request import Request
|
82
|
+
|
83
|
+
|
86
84
|
|
87
85
|
class MyHTTPConnection(HTTPSConnection):
|
88
86
|
|
3
追加
test
CHANGED
@@ -88,7 +88,7 @@
|
|
88
88
|
|
89
89
|
def send(self, s):
|
90
90
|
|
91
|
-
print("[[request_header:]]\n%s" % s.decode())
|
91
|
+
print("[[request_header:]]\n%s" % s.decode())
|
92
92
|
|
93
93
|
HTTPSConnection.send(self, s)
|
94
94
|
|
@@ -98,15 +98,7 @@
|
|
98
98
|
|
99
99
|
def https_open(self, req):
|
100
100
|
|
101
|
-
# print(req.__dict__)
|
102
|
-
|
103
101
|
return self.do_open(MyHTTPConnection, req)
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
102
|
|
111
103
|
|
112
104
|
|
@@ -119,8 +111,6 @@
|
|
119
111
|
opener = build_opener(MyHTTPHandler)
|
120
112
|
|
121
113
|
_ = opener.open(url)
|
122
|
-
|
123
|
-
# print("response_header: %s" % (response))
|
124
114
|
|
125
115
|
|
126
116
|
|
2
追加
test
CHANGED
@@ -63,3 +63,77 @@
|
|
63
63
|
|
64
64
|
|
65
65
|
参考:https://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib2-urlopen-call
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
# 追記
|
70
|
+
|
71
|
+
HTTPSの場合は下記。
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
import os,pprint
|
76
|
+
|
77
|
+
from urllib.request import HTTPSHandler, build_opener, urlretrieve
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
from http.client import HTTPSConnection
|
84
|
+
|
85
|
+
from urllib.request import Request
|
86
|
+
|
87
|
+
class MyHTTPConnection(HTTPSConnection):
|
88
|
+
|
89
|
+
def send(self, s):
|
90
|
+
|
91
|
+
print("[[request_header:]]\n%s" % s.decode()) # or save them, or whatever!
|
92
|
+
|
93
|
+
HTTPSConnection.send(self, s)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
class MyHTTPHandler(HTTPSHandler):
|
98
|
+
|
99
|
+
def https_open(self, req):
|
100
|
+
|
101
|
+
# print(req.__dict__)
|
102
|
+
|
103
|
+
return self.do_open(MyHTTPConnection, req)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def download_file(url):
|
114
|
+
|
115
|
+
dst_path=os.path.basename(url)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
opener = build_opener(MyHTTPHandler)
|
120
|
+
|
121
|
+
_ = opener.open(url)
|
122
|
+
|
123
|
+
# print("response_header: %s" % (response))
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
_, mes = urlretrieve(url, dst_path)
|
128
|
+
|
129
|
+
print("[[response_header]]: \n%s" % mes)
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
download_file("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Hummingbird.jpg/800px-Hummingbird.jpg")
|
134
|
+
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
大体のことは公式ドキュメント(https://docs.python.org/ja/3/library/urllib.request.html、 https://docs.python.org/ja/3/library/http.client.html)に書いてあります。
|
1
追加
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
def send(self, s):
|
22
22
|
|
23
|
-
print("[[request_header:]]\n%s" % s.decode())
|
23
|
+
print("[[request_header:]]\n%s" % s.decode())
|
24
24
|
|
25
25
|
HTTPConnection.send(self, s)
|
26
26
|
|
@@ -59,3 +59,7 @@
|
|
59
59
|
download_file('http://www.example.com/')
|
60
60
|
|
61
61
|
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
参考:https://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib2-urlopen-call
|