質問編集履歴
2
変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
imgタグ
|
1
|
+
htmlのimgタグのsrc属性内の「http」で始まる文のみを正規表現で取得する方法
|
test
CHANGED
@@ -1,10 +1,36 @@
|
|
1
1
|
サイトから画像をスレイピングしたいのですが[対象のサイト](http://www.asos.com/search/dress?page=1&q=dress)はgoogle chromeで見たところ、imgタグのsrc属性に.jpgなのど拡張子の要素がありません。どうやら画像のリンクがあるようです。
|
2
2
|
|
3
|
-
|
3
|
+
そこでimgダグのsrc属性ないの「http」で始まる文章を正規表現で取得したのですが、pythonの正規表現でうまい取得の仕方はないでしょうか?
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
|
7
|
+
imgタグ内のsrc属性内はhttpで始まる文章の後に 空行が入って400pxが書いてあり、また空行を挟んでhttpで始まる文章が繰り返されています
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
例:http~ 400px http~ 200px http~ 300px http~
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
このような文章でimgタグのsrc属性内のhttpの文章のみ取得する正規表現がどのように書けばいいのでしょうか?
|
18
|
+
|
19
|
+
下の正規表現だとうまくいかないようなので、ご教授お願いします
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
#該当正規表現
|
24
|
+
|
25
|
+
```python
|
26
|
+
|
27
|
+
for link in soup.find_all('img'):
|
28
|
+
|
29
|
+
images.append(urljoin(URL, link.get('src'=re.compile('^http.*$'))))
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
|
8
34
|
|
9
35
|
|
10
36
|
|
@@ -18,7 +44,7 @@
|
|
18
44
|
|
19
45
|
import re
|
20
46
|
|
21
|
-
|
47
|
+
|
22
48
|
|
23
49
|
|
24
50
|
|
@@ -34,34 +60,4 @@
|
|
34
60
|
|
35
61
|
images.append(urljoin(URL, link.get('src'=re.compile('^http.*$'))))
|
36
62
|
|
37
|
-
|
38
|
-
|
39
|
-
for target in images: # imagesからtargetに入れる
|
40
|
-
|
41
|
-
re = requests.get(target)
|
42
|
-
|
43
|
-
with open('/Users/Downloads/img/' + target.split('/')[-1], 'wb') as f: # imgフォルダに格納
|
44
|
-
|
45
|
-
f.write(re.content) # .contentにて画像データとして書き込む
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
print("ok") # 確認
|
50
|
-
|
51
63
|
```
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
#エラー
|
56
|
-
|
57
|
-
```
|
58
|
-
|
59
|
-
File "scrapying.py", line 11
|
60
|
-
|
61
|
-
images.append(urljoin(URL, link.get('src'=re.compile('^http.*$'))))
|
62
|
-
|
63
|
-
^
|
64
|
-
|
65
|
-
SyntaxError: keyword can't be an expression
|
66
|
-
|
67
|
-
```
|
1
質問変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
imgタグに.jpg要素がないサイトの画像スレイピングのコードエラーについて
|
test
CHANGED
@@ -1,173 +1,67 @@
|
|
1
|
+
サイトから画像をスレイピングしたいのですが[対象のサイト](http://www.asos.com/search/dress?page=1&q=dress)はgoogle chromeで見たところ、imgタグのsrc属性に.jpgなのど拡張子の要素がありません。どうやら画像のリンクがあるようです。
|
2
|
+
|
1
|
-
|
3
|
+
BeautifulSoupを使い、かつ正規表現でスクレイピングをしようとしたのですが、うまくいきません。
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
編集画面はソースコード(html)で編集可能のですが[この事例](https://blog.makky.io/articles/2017/06/25/mturk-jp/)のように、条件に合わせて一枚の画像にtrainかtestの2つチェックボックスを設置し、1000枚程度の画像をラベリングしてもらうことを考えています。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
条件文は後で記すとして、そもそも画像を1000枚もチェックボックスを設置してラベリングするようにできるようなhtmlはどのようにするのでしょうか?
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
htmlで編集するのではなく、画像はurlでアップロードですが、そもそもローカルにある画像ですし、Dropboxで共有ファルダを作ってURLを貼ってもうまくいきません。
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
7
|
+
正規表現が間違っている以外にも、正常に画像がスクレイピングできるかわかりません。このような.jpg拡張子がないサイトはスクレイピングできるのでしょうか?訂正箇所の、ご教授お願いします
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
何かご存知でしたらご教授お願いします。
|
24
8
|
|
25
9
|
|
26
10
|
|
27
11
|
# ソースコード
|
28
12
|
|
29
|
-
```h
|
13
|
+
```python
|
30
14
|
|
31
|
-
|
15
|
+
import requests
|
32
16
|
|
33
|
-
|
17
|
+
from requests.compat import urljoin
|
34
18
|
|
35
|
-
|
19
|
+
import re
|
36
20
|
|
37
|
-
|
38
|
-
|
39
|
-
<div class="row">
|
40
|
-
|
41
|
-
<div class="col-xs-12 col-md-12"><!-- Instructions -->
|
42
|
-
|
43
|
-
<div class="panel panel-primary"><!-- WARNING: the ids "collapseTrigger" and "instructionBody" are being used to enable expand/collapse feature --><a class="panel-heading" href="javascript:void(0);" id="collapseTrigger"><strong>Image Tagging Instructions</strong> <span class="collapse-text">(Click to expand)</span> </a>
|
44
|
-
|
45
|
-
<div class="panel-body" id="instructionBody">Select train image and test image bleow pilincipls:</div>
|
21
|
+
from bs4 import BeautifulSoup
|
46
22
|
|
47
23
|
|
48
24
|
|
49
|
-
|
25
|
+
URL = 'http://www.asos.com/search/dress?page=1&q=dress' # URL入力
|
50
26
|
|
51
|
-
|
27
|
+
images = [] # 画像リストの配列
|
52
|
-
|
53
|
-
</div>
|
54
|
-
|
55
|
-
</div>
|
56
|
-
|
57
|
-
<!-- End Instructions --><!-- Image Tagging Layout -->
|
58
28
|
|
59
29
|
|
60
30
|
|
61
|
-
|
31
|
+
soup = BeautifulSoup(requests.get(URL).content,'lxml') # bsでURL内を解析
|
62
32
|
|
63
|
-
|
33
|
+
for link in soup.find_all('img'):
|
34
|
+
|
35
|
+
images.append(urljoin(URL, link.get('src'=re.compile('^http.*$'))))
|
64
36
|
|
65
37
|
|
66
38
|
|
67
|
-
|
39
|
+
for target in images: # imagesからtargetに入れる
|
68
40
|
|
41
|
+
re = requests.get(target)
|
42
|
+
|
69
|
-
|
43
|
+
with open('/Users/Downloads/img/' + target.split('/')[-1], 'wb') as f: # imgフォルダに格納
|
44
|
+
|
45
|
+
f.write(re.content) # .contentにて画像データとして書き込む
|
70
46
|
|
71
47
|
|
72
48
|
|
73
|
-
|
49
|
+
print("ok") # 確認
|
50
|
+
|
51
|
+
```
|
74
52
|
|
75
53
|
|
76
54
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
<div class="form-group"><label for="tag3">Tag 3:None</label><input class="form-control" id="tag3" maxlength="20" name="tag3" required="" size="30" type="text" value="2" /></div>
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
<div class="form-group"><label class="group-label">Tag 4:</label>
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
<div class="radio"><label><input autocomplete="off" id="option1" name="Tag4" required="" type="radio" value="yes" /> Option 1 </label></div>
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
<div class="radio"><label><input autocomplete="off" id="option2" name="Tag4" required="" type="radio" value="no" /> Option 2 </label></div>
|
94
|
-
|
95
|
-
</div>
|
96
|
-
|
97
|
-
</div>
|
98
|
-
|
99
|
-
</div>
|
100
|
-
|
101
|
-
</section>
|
102
|
-
|
103
|
-
<!-- End Image Tagging Layout --><!-- Open internal style sheet -->
|
104
|
-
|
105
|
-
<style type="text/css">#collapseTrigger{
|
106
|
-
|
107
|
-
color:#fff;
|
108
|
-
|
109
|
-
display: block;
|
110
|
-
|
111
|
-
text-decoration: none;
|
112
|
-
|
113
|
-
}
|
114
|
-
|
115
|
-
#submitButton{
|
116
|
-
|
117
|
-
white-space: normal;
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
.image{
|
122
|
-
|
123
|
-
margin-bottom: 15px;
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
.radio:first-of-type{
|
128
|
-
|
129
|
-
margin-top: -5px;
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
</style>
|
134
|
-
|
135
|
-
<!-- Close internal style sheet --><!-- Please note that Bootstrap CSS/JS and JQuery are 3rd party libraries that may update their url/code at any time. Amazon Mechanical Turk (MTurk) is including these libraries as a default option for you, but is not responsible for any changes to the external libraries --><script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js" integrity="sha384-s1ITto93iSMDxlp/79qhWHi+LsIi9Gx6yL+cOKDuymvihkfol83TYbLbOw+W/wv4" crossorigin="anonymous"></script><script>
|
136
|
-
|
137
|
-
$(document).ready(function() {
|
138
|
-
|
139
|
-
// Instructions expand/collapse
|
140
|
-
|
141
|
-
var content = $('#instructionBody');
|
142
|
-
|
143
|
-
var trigger = $('#collapseTrigger');
|
144
|
-
|
145
|
-
content.hide();
|
146
|
-
|
147
|
-
$('.collapse-text').text('(Click to expand)');
|
148
|
-
|
149
|
-
trigger.click(function(){
|
150
|
-
|
151
|
-
content.toggle();
|
152
|
-
|
153
|
-
var isVisible = content.is(':visible');
|
154
|
-
|
155
|
-
if(isVisible){
|
156
|
-
|
157
|
-
$('.collapse-text').text('(Click to collapse)');
|
158
|
-
|
159
|
-
}else{
|
160
|
-
|
161
|
-
$('.collapse-text').text('(Click to expand)');
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
});
|
166
|
-
|
167
|
-
// end expand/collapse
|
168
|
-
|
169
|
-
});
|
170
|
-
|
171
|
-
</script>
|
55
|
+
#エラー
|
172
56
|
|
173
57
|
```
|
58
|
+
|
59
|
+
File "scrapying.py", line 11
|
60
|
+
|
61
|
+
images.append(urljoin(URL, link.get('src'=re.compile('^http.*$'))))
|
62
|
+
|
63
|
+
^
|
64
|
+
|
65
|
+
SyntaxError: keyword can't be an expression
|
66
|
+
|
67
|
+
```
|