回答編集履歴
4
Pathlib.mkdirを使うように変更!
answer
CHANGED
@@ -88,7 +88,7 @@
|
|
88
88
|
# sec
|
89
89
|
self.timeout = 10
|
90
90
|
self.download_dir = Path('download')
|
91
|
-
|
91
|
+
self.download_dir.mkdir(exist_ok=True)
|
92
92
|
|
93
93
|
def get(self, url: str, params: dict=None):
|
94
94
|
# 実際のリクエスト処理はここ
|
3
追記
answer
CHANGED
@@ -74,7 +74,7 @@
|
|
74
74
|
def search(self):
|
75
75
|
search_word = self.key_word.get()
|
76
76
|
# ※画面をブロックさせないためにスレッドを生成して、startを呼び出す。
|
77
|
-
t = Thread(target=hack,args=search_word)
|
77
|
+
t = Thread(target=hack, args=(search_word,))
|
78
78
|
t.start()
|
79
79
|
|
80
80
|
def owari(self):
|
2
追記
answer
CHANGED
@@ -48,6 +48,10 @@
|
|
48
48
|
# ロゴ?を表示
|
49
49
|
# image1 = tk.PhotoImage(file = 'LOGO.gif')
|
50
50
|
# tk.Label(frame1, image = image1).pack()#.grid(row=0, column=0)
|
51
|
+
# ※検索キーワードのテキストボックスを作成
|
52
|
+
self.key_word = tk.StringVar(value='草')
|
53
|
+
self.txt_key_word = tk.Entry(self, textvariable=self.key_word)
|
54
|
+
self.txt_key_word.pack()
|
51
55
|
# 探すボタンを作成
|
52
56
|
serch = tk.Button(self, text='探す', padx=45, pady=7, command=self.search)
|
53
57
|
serch.pack()
|
@@ -68,8 +72,9 @@
|
|
68
72
|
owa.pack()
|
69
73
|
|
70
74
|
def search(self):
|
75
|
+
search_word = self.key_word.get()
|
71
76
|
# ※画面をブロックさせないためにスレッドを生成して、startを呼び出す。
|
72
|
-
t = Thread(target=hack)
|
77
|
+
t = Thread(target=hack,args=search_word)
|
73
78
|
t.start()
|
74
79
|
|
75
80
|
def owari(self):
|
@@ -144,9 +149,9 @@
|
|
144
149
|
|
145
150
|
|
146
151
|
#ダウンロード用(HTML)関数
|
147
|
-
def hack(): #wordeで取得したURLから画像のURLを抜き出す(解析) 使用ライブラリ:bs4
|
152
|
+
def hack(search_word: str): #wordeで取得したURLから画像のURLを抜き出す(解析) 使用ライブラリ:bs4
|
148
153
|
url = 'https://search.yahoo.co.jp/image/search'
|
149
|
-
params = {'n': '60', 'p':
|
154
|
+
params = {'n': '60', 'p': search_word, 'search.x': '1'}
|
150
155
|
res = dl.get(url, params)
|
151
156
|
print(res.text)
|
152
157
|
soup = bs4.BeautifulSoup(res.text, "html.parser") #わかんね
|
1
補足
answer
CHANGED
@@ -1,20 +1,35 @@
|
|
1
|
+
> bs4で画像のURLを取得する方法が解らないです。
|
2
|
+
|
3
|
+
これに関しては質問文のソースコードでurl_listでURLを取得できているので、ダウンロードの仕方と保存の仕方が分からないと判断しました。
|
4
|
+
|
5
|
+
極論を言えば以下のコードでも可能かと。
|
6
|
+
```Python
|
7
|
+
import os
|
8
|
+
|
9
|
+
for s in url_list:
|
10
|
+
res = requests.get(s)
|
11
|
+
file_name = os.path.basename(s)
|
12
|
+
with open(file_name, 'wb') as f:
|
13
|
+
f.write(res.content)
|
14
|
+
```
|
15
|
+
|
16
|
+
---
|
17
|
+
|
1
18
|
> ※超初心者です。
|
2
19
|
|
3
20
|
うーん、少し悩みましたが、ソースコードを頑張って読み取ってくれることを期待して。
|
4
|
-
|
5
21
|
|クラス|役割|
|
6
22
|
|:--|:--:|
|
7
23
|
|MyFrame|画面|
|
8
24
|
|DownLoader|ダウンロード処理|
|
9
25
|
|
10
|
-
|
11
26
|
```Python
|
12
27
|
# -*- coding: utf-8 -*-
|
13
28
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
14
29
|
from io import BytesIO
|
15
|
-
from threading import Thread
|
16
30
|
import os
|
17
31
|
from pathlib import Path
|
32
|
+
from threading import Thread
|
18
33
|
from time import sleep
|
19
34
|
#おまじない
|
20
35
|
import urllib #URLエンコード用
|
@@ -67,6 +82,8 @@
|
|
67
82
|
self.interval = 500 / 1000
|
68
83
|
# sec
|
69
84
|
self.timeout = 10
|
85
|
+
self.download_dir = Path('download')
|
86
|
+
os.makedirs(str(self.download_dir), exist_ok=True)
|
70
87
|
|
71
88
|
def get(self, url: str, params: dict=None):
|
72
89
|
# 実際のリクエスト処理はここ
|
@@ -93,17 +110,19 @@
|
|
93
110
|
for future in as_completed(future_to_url):
|
94
111
|
url = future_to_url[future]
|
95
112
|
try:
|
96
|
-
|
113
|
+
|
97
|
-
# 重複が発生するので連番を付けたりして対応してくださいな。
|
98
|
-
basename = os.path.basename(url)
|
99
114
|
# get_contentの戻り値はここで取得
|
100
115
|
buffer, content_type = future.result()
|
101
116
|
# 保存対象のファイルかどうか。
|
102
117
|
if not self.save_content_type(content_type):
|
103
118
|
continue
|
119
|
+
# 保存ファイル名はURLのパス部分をそのまま取得
|
120
|
+
# 重複が発生するので連番を付けたりして対応してくださいな。
|
121
|
+
file_name = self.download_dir / os.path.basename(url)
|
122
|
+
|
104
|
-
print(content_type,
|
123
|
+
print(content_type, file_name)
|
105
124
|
# 保存
|
106
|
-
self.save_file(buffer,
|
125
|
+
self.save_file(buffer, file_name)
|
107
126
|
count += 1
|
108
127
|
except Exception as ex:
|
109
128
|
print(f"url:{url}, {ex}")
|
@@ -115,8 +134,8 @@
|
|
115
134
|
is_saved = ["image/jpeg", "image/png", "image/gif"]
|
116
135
|
return content_type.lower() in is_saved
|
117
136
|
|
118
|
-
def save_file(self, buffer: BytesIO, file_name:
|
137
|
+
def save_file(self, buffer: BytesIO, file_name: Path) ->None:
|
119
|
-
with open(
|
138
|
+
with file_name.open('wb') as f:
|
120
139
|
f.write(buffer.getvalue())
|
121
140
|
|
122
141
|
|