teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

6

2020/12/22 03:30

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -58,5 +58,5 @@
58
58
  _, mes = urlretrieve(url, dst_path)
59
59
  print("[[response_header]]: \n%s" % mes)
60
60
 
61
- download_file("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Hummingbird.jpg/800px-Hummingbird.jpg")
61
+ download_file('https://www.example.com/')
62
62
  ```

5

2020/12/22 03:30

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -59,6 +59,4 @@
59
59
  print("[[response_header]]: \n%s" % mes)
60
60
 
61
61
  download_file("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Hummingbird.jpg/800px-Hummingbird.jpg")
62
- ```
62
+ ```
63
-
64
- 大体のことは公式ドキュメント(https://docs.python.org/ja/3/library/urllib.request.html、 https://docs.python.org/ja/3/library/http.client.html)に書いてあります。

4

修正

2020/12/22 03:29

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -37,10 +37,9 @@
37
37
  ```
38
38
  import os,pprint
39
39
  from urllib.request import HTTPSHandler, build_opener, urlretrieve
40
-
41
-
42
40
  from http.client import HTTPSConnection
43
41
  from urllib.request import Request
42
+
44
43
  class MyHTTPConnection(HTTPSConnection):
45
44
  def send(self, s):
46
45
  print("[[request_header:]]\n%s" % s.decode())

3

追加

2020/12/22 03:16

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -43,23 +43,18 @@
43
43
  from urllib.request import Request
44
44
  class MyHTTPConnection(HTTPSConnection):
45
45
  def send(self, s):
46
- print("[[request_header:]]\n%s" % s.decode()) # or save them, or whatever!
46
+ print("[[request_header:]]\n%s" % s.decode())
47
47
  HTTPSConnection.send(self, s)
48
48
 
49
49
  class MyHTTPHandler(HTTPSHandler):
50
50
  def https_open(self, req):
51
- # print(req.__dict__)
52
51
  return self.do_open(MyHTTPConnection, req)
53
52
 
54
-
55
-
56
-
57
53
  def download_file(url):
58
54
  dst_path=os.path.basename(url)
59
55
 
60
56
  opener = build_opener(MyHTTPHandler)
61
57
  _ = opener.open(url)
62
- # print("response_header: %s" % (response))
63
58
 
64
59
  _, mes = urlretrieve(url, dst_path)
65
60
  print("[[response_header]]: \n%s" % mes)

2

追加

2020/12/22 03:15

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -30,4 +30,41 @@
30
30
  download_file('http://www.example.com/')
31
31
  ```
32
32
 
33
- 参考:https://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib2-urlopen-call
33
+ 参考:https://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib2-urlopen-call
34
+
35
+ # 追記
36
+ HTTPSの場合は下記。
37
+ ```
38
+ import os,pprint
39
+ from urllib.request import HTTPSHandler, build_opener, urlretrieve
40
+
41
+
42
+ from http.client import HTTPSConnection
43
+ from urllib.request import Request
44
+ class MyHTTPConnection(HTTPSConnection):
45
+ def send(self, s):
46
+ print("[[request_header:]]\n%s" % s.decode()) # or save them, or whatever!
47
+ HTTPSConnection.send(self, s)
48
+
49
+ class MyHTTPHandler(HTTPSHandler):
50
+ def https_open(self, req):
51
+ # print(req.__dict__)
52
+ return self.do_open(MyHTTPConnection, req)
53
+
54
+
55
+
56
+
57
+ def download_file(url):
58
+ dst_path=os.path.basename(url)
59
+
60
+ opener = build_opener(MyHTTPHandler)
61
+ _ = opener.open(url)
62
+ # print("response_header: %s" % (response))
63
+
64
+ _, mes = urlretrieve(url, dst_path)
65
+ print("[[response_header]]: \n%s" % mes)
66
+
67
+ download_file("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/Hummingbird.jpg/800px-Hummingbird.jpg")
68
+ ```
69
+
70
+ 大体のことは公式ドキュメント(https://docs.python.org/ja/3/library/urllib.request.html、 https://docs.python.org/ja/3/library/http.client.html)に書いてあります。

1

追加

2020/12/22 03:14

投稿

sfdust
sfdust

スコア1137

answer CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  class MyHTTPConnection(HTTPConnection):
11
11
  def send(self, s):
12
- print("[[request_header:]]\n%s" % s.decode()) # or save them, or whatever!
12
+ print("[[request_header:]]\n%s" % s.decode())
13
13
  HTTPConnection.send(self, s)
14
14
 
15
15
  class MyHTTPHandler(HTTPHandler):
@@ -28,4 +28,6 @@
28
28
 
29
29
 
30
30
  download_file('http://www.example.com/')
31
- ```
31
+ ```
32
+
33
+ 参考:https://stackoverflow.com/questions/843392/python-get-http-headers-from-urllib2-urlopen-call