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

質問編集履歴

1

vbaコードを更新しました。 divタグ内にあるdata-asinの値の抽出方法を教えてください。宜しくお願い致します。

2020/04/16 14:44

投稿

yking
yking

スコア12

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
  一通りググってみましたが、わからず行き詰っております。
5
5
  よろしくお願いします。
6
6
 
7
- vba(selenium chrome)スクレイピングで、
7
+ vbaスクレイピングで、
8
8
  amazon検索一覧URLから、商品のASINだけを抽出したい。ページおくりで、検索の最後まで、一気に抽出したい。
9
9
  (参考:https://www.amazon.co.jp/s?bbn=170092011&rh=n%3A52374051%2Cn%3A%2152391051%2Cn%3A5267100051%2Cn%3A170040011%2Cn%3A170092011%2Cp_76%3A2227292051&dc&fst=as%3Aoff&qid=1586994278&rnid=2227291051&ref=lp_170092011_nr_p_76_0
10
10
 
@@ -14,36 +14,61 @@
14
14
  参考https://excel-ubara.com/excelvba4/EXCEL_VBA_401.html#にして、
15
15
  下記コードまで記述しました。
16
16
 
17
-
18
- Sub main()
17
+ Sub testIE()
18
+ Dim I As Integer
19
+ Dim objIE As InternetExplorer 'IEオブジェクトを準備
20
+ Set objIE = CreateObject("Internetexplorer.Application") '新しいIEオブジェクトを作成してセット
19
21
 
20
- Dim Driver As New Selenium.WebDriver
21
- Driver.Start "chrome"
22
- Driver.Get "https://www.amazon.co.jp/s?bbn=170092011&rh=n%3A52374051%2Cn%3A%2152391051%2Cn%3A5267100051%2Cn%3A170040011%2Cn%3A170092011%2Cp_76%3A2227292051&dc&fst=as%3Aoff&qid=1586994278&rnid=2227291051&ref=lp_170092011_nr_p_76_0"
23
- Driver.Get "/"
22
+ objIE.Visible = True 'IEを表示
24
23
 
24
+ I = 2
25
+ Do
26
+ If Range("A" & I) = "" Then Exit Do
25
- '分からない部分↓
27
+ If I > 100 Then Exit Do
28
+
29
+ objIE.navigate Range("A" & I) 'IEでURLを開く
30
+
31
+ Call WaitResponse(objIE) '読み込み待ち
26
32
 
27
- Dim txt As String
28
- txt = Driver.FindElementByCss(" ")
33
+ Dim objDoc As HTMLElementCollection
29
- Debug.Print txt
30
- Driver.Close
31
- Set Driver = Nothing
34
+ Set objDoc = objIE.document
32
-
35
+ Dim element As IHTMLElement
36
+ For Each element In objDoc.getElementsByClassName("sg-col-4-of-24 sg-col-4-of-12 sg-col-4-of-36 s-result-item s-asin sg-col-4-of-28 sg-col-4-of-16 sg-col sg-col-4-of-20 sg-col-4-of-32")
37
+ Debug.Print element. 'わからない部分!!!!!
38
+
39
+
40
+
41
+ Next element
42
+ I = I + 1
43
+ Loop
44
+
45
+ Call WaitResponse(objIE)
33
46
  End Sub
34
47
 
35
48
 
36
49
 
37
- 肝心のASIN抽出処理部分ですが、HTMLでASINコードが入っているのが href要素かと思い、様々な方法で記述してみましたが、エラーがでます。
38
- 抽出処理の仕方はどのようにすれば良いのか教えて頂けませんでしょうか。
50
+ Sub WaitResponse(objIE As Object)
51
+ Do While objIE.Busy = True Or objIE.readyState < READYSTATE_COMPLETE '読み込み待ち
39
- よろしくお願いします。
52
+ DoEvents
53
+ Loop
54
+ End Sub
40
55
 
41
56
 
42
- <div class="sg-col-inner">
57
+
58
+
59
+
60
+ 肝心のASIN抽出処理部分ですが、HTMLでASINコードが入っているのが
61
+ 下記divタグ内のdata-asin=にあるので、getElementByclassNameで取得をしようとしているのですが、
62
+ data-asin=””にあるASINコードはどのようにすると抽出できますか??
63
+ 宜しくお願い致します。
64
+
65
+ <div ****data-asin="B07HVKS8DM"**** data-index="9" data-uuid="644bd227-abf2-4d00-9fc9-6b8be26f583b" class="sg-col-4-of-24 sg-col-4-of-12 sg-col-4-of-36 s-result-item s-asin sg-col-4-of-28 sg-col-4-of-16 sg-col sg-col-4-of-20 sg-col-4-of-32 __web-inspector-hide-shortcut__" data-cel-widget="search_result_9"><div class="sg-col-inner">
43
66
 
44
67
 
45
68
 
69
+
70
+
46
- <span cel_widget_id="SEARCH_RESULTS-SEARCH_RESULTS" class="celwidget slot=SEARCH_RESULTS template=SEARCH_RESULTS widgetId=search-results index=1" data-cel-widget="SEARCH_RESULTS-SEARCH_RESULTS">
71
+ <span cel_widget_id="SEARCH_RESULTS-SEARCH_RESULTS" class="celwidget slot=SEARCH_RESULTS template=SEARCH_RESULTS widgetId=search-results index=9" data-cel-widget="SEARCH_RESULTS-SEARCH_RESULTS">
47
72
 
48
73
 
49
74
 
@@ -72,16 +97,175 @@
72
97
 
73
98
 
74
99
 
75
- <span data-component-type="s-product-image" class="rush-component" data-component-id="11">
100
+ <span data-component-type="s-product-image" class="rush-component" data-component-id="17">
76
101
 
77
- <a class="a-link-normal" target="_blank" href="/SKINAUTHORITY-%E3%82%B9%E3%82%AD%E3%83%B3%E3%82%AA%E3%83%BC%E3%82%BD%E3%83%AA%E3%83%86%E3%82%A3%E3%83%BC-%E3%80%90Amazon-co-jp%E9%99%90%E5%AE%9A%E3%80%91SKIN-AUTHORITY-%E3%83%8F%E3%83%88%E3%83%A0%E3%82%AE%E5%8C%96%E7%B2%A7%E6%B0%B4/dp/B07CY6M11L/ref=sr_1_2?dchild=1&amp;fst=as%3Aoff&amp;qid=1587002028&amp;refinements=p_76%3A2227292051&amp;rnid=2227291051&amp;rps=1&amp;s=beauty&amp;sr=1-2">
102
+ <a class="a-link-normal" target="_blank" href="/%E6%A9%9F%E5%8B%95%E6%88%A6%E5%A3%AB%E3%82%AC%E3%83%B3%E3%83%80%E3%83%A0-MS-09-MS-09R-%E3%83%AA%E3%83%83%E3%82%AF%E3%83%BB%E3%83%89%E3%83%A0-144%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB/dp/B07HVKS8DM/ref=sr_1_10?dchild=1&amp;fst=as%3Aoff&amp;qid=1586877663&amp;refinements=p_n_feature_fifteen_browse-bin%3A3307612051%2Cp_76%3A2227292051&amp;rnid=2227291051&amp;rps=1&amp;s=hobby&amp;sr=1-10">
78
103
  <div class="a-section aok-relative s-image-square-aspect">
79
104
 
80
105
 
81
106
 
82
- <img src="https://m.media-amazon.com/images/I/71AfoRQ5rVL._AC_UL320_ML3_.jpg" class="s-image" alt="[Amazon限定ブラド] SKIN AUTHORITY ハトギ化粧水 1000ml" srcset="https://m.media-amazon.com/images/I/71AfoRQ5rVL._AC_UL320_ML3_.jpg 1x, https://m.media-amazon.com/images/I/71AfoRQ5rVL._AC_UL480_FMwebp_QL65_.jpg 1.5x, https://m.media-amazon.com/images/I/71AfoRQ5rVL._AC_UL640_FMwebp_QL65_.jpg 2x, https://m.media-amazon.com/images/I/71AfoRQ5rVL._AC_UL800_FMwebp_QL65_.jpg 2.5x, https://m.media-amazon.com/images/I/71AfoRQ5rVL._AC_UL960_FMwebp_QL65_.jpg 3x" data-image-index="1" data-image-load="" data-image-latency="s-product-image" data-image-source-density="1">
107
+ <img src="https://m.media-amazon.com/images/I/61tIVRyF7qL._AC_UL320_ML3_.jpg" class="s-image" alt="HGUC 機動戦士ガダム MS-09 ドム/MS-09R リック・ド1/144スケール プラモデル" srcset="https://m.media-amazon.com/images/I/61tIVRyF7qL._AC_UL320_ML3_.jpg 1x, https://m.media-amazon.com/images/I/61tIVRyF7qL._AC_UL480_FMwebp_QL65_.jpg 1.5x, https://m.media-amazon.com/images/I/61tIVRyF7qL._AC_UL640_FMwebp_QL65_.jpg 2x, https://m.media-amazon.com/images/I/61tIVRyF7qL._AC_UL800_FMwebp_QL65_.jpg 2.5x, https://m.media-amazon.com/images/I/61tIVRyF7qL._AC_UL960_FMwebp_QL65_.jpg 3x" data-image-index="9" data-image-load="" data-image-latency="s-product-image" data-image-source-density="1">
83
108
 
84
109
 
85
110
  </div>
86
111
  </a>
87
- </span>
112
+ </span>
113
+
114
+
115
+
116
+
117
+
118
+ <div class="a-section a-spacing-none a-spacing-top-small">
119
+
120
+
121
+
122
+
123
+
124
+ <h2 class="a-size-mini a-spacing-none a-color-base s-line-clamp-4">
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+ <a class="a-link-normal a-text-normal" target="_blank" href="/%E6%A9%9F%E5%8B%95%E6%88%A6%E5%A3%AB%E3%82%AC%E3%83%B3%E3%83%80%E3%83%A0-MS-09-MS-09R-%E3%83%AA%E3%83%83%E3%82%AF%E3%83%BB%E3%83%89%E3%83%A0-144%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB/dp/B07HVKS8DM/ref=sr_1_10?dchild=1&amp;fst=as%3Aoff&amp;qid=1586877663&amp;refinements=p_n_feature_fifteen_browse-bin%3A3307612051%2Cp_76%3A2227292051&amp;rnid=2227291051&amp;rps=1&amp;s=hobby&amp;sr=1-10">
133
+
134
+
135
+
136
+ <span class="a-size-base-plus a-color-base a-text-normal">HGUC 機動戦士ガンダム MS-09 ドム/MS-09R リック・ドム 1/144スケール プラモデル</span>
137
+
138
+
139
+
140
+
141
+ </a>
142
+
143
+
144
+ </h2>
145
+
146
+ </div>
147
+
148
+ <div class="a-section a-spacing-none a-spacing-top-micro">
149
+ <div class="a-row a-size-small">
150
+
151
+
152
+ <span aria-label="5つ星のうち4.5">
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ <span class="a-declarative" data-action="a-popover" data-a-popover="{&quot;max-width&quot;:&quot;700&quot;,&quot;closeButton&quot;:false,&quot;position&quot;:&quot;triggerBottom&quot;,&quot;url&quot;:&quot;/review/widgets/average-customer-review/popover/ref=acr_search__popover?ie=UTF8&amp;asin=B07HVKS8DM&amp;ref=acr_search__popover&amp;contextId=search&quot;}">
162
+
163
+ <a href="javascript:void(0)" class="a-popover-trigger a-declarative"><i class="a-icon a-icon-star-small a-star-small-4-5 aok-align-bottom"><span class="a-icon-alt">5つ星のうち4.5</span></i><i class="a-icon a-icon-popover"></i></a>
164
+ </span>
165
+
166
+
167
+
168
+
169
+ </span>
170
+
171
+
172
+
173
+ <span aria-label="36">
174
+
175
+
176
+
177
+
178
+
179
+ <a class="a-link-normal" target="_blank" href="/%E6%A9%9F%E5%8B%95%E6%88%A6%E5%A3%AB%E3%82%AC%E3%83%B3%E3%83%80%E3%83%A0-MS-09-MS-09R-%E3%83%AA%E3%83%83%E3%82%AF%E3%83%BB%E3%83%89%E3%83%A0-144%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB/dp/B07HVKS8DM/ref=sr_1_10?dchild=1&amp;fst=as%3Aoff&amp;qid=1586877663&amp;refinements=p_n_feature_fifteen_browse-bin%3A3307612051%2Cp_76%3A2227292051&amp;rnid=2227291051&amp;rps=1&amp;s=hobby&amp;sr=1-10#customerReviews">
180
+
181
+
182
+
183
+ <span class="a-size-base">36</span>
184
+
185
+
186
+
187
+
188
+ </a>
189
+
190
+ </span>
191
+ </div>
192
+ </div>
193
+
194
+
195
+
196
+
197
+ <div class="a-section a-spacing-none a-spacing-top-small">
198
+ <div class="a-row a-size-base a-color-base"><div class="a-row">
199
+
200
+
201
+
202
+
203
+ <a class="a-size-base a-link-normal s-no-hover a-text-normal" target="_blank" href="/%E6%A9%9F%E5%8B%95%E6%88%A6%E5%A3%AB%E3%82%AC%E3%83%B3%E3%83%80%E3%83%A0-MS-09-MS-09R-%E3%83%AA%E3%83%83%E3%82%AF%E3%83%BB%E3%83%89%E3%83%A0-144%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%AB/dp/B07HVKS8DM/ref=sr_1_10?dchild=1&amp;fst=as%3Aoff&amp;qid=1586877663&amp;refinements=p_n_feature_fifteen_browse-bin%3A3307612051%2Cp_76%3A2227292051&amp;rnid=2227291051&amp;rps=1&amp;s=hobby&amp;sr=1-10">
204
+
205
+
206
+
207
+ <span class="a-price" data-a-size="m" data-a-color="price"><span class="a-offscreen">¥2,480</span><span aria-hidden="true"><span class="a-price-symbol"></span><span class="a-price-whole">¥2,480</span></span></span>
208
+
209
+
210
+
211
+
212
+ </a>
213
+ </div></div>
214
+ </div>
215
+
216
+
217
+ <div class="a-section a-spacing-none a-spacing-top-micro">
218
+ <div class="a-row a-size-base a-color-secondary s-align-children-center"><div class="a-row s-align-children-center">
219
+
220
+
221
+
222
+
223
+ <span class="aok-inline-block s-image-logo-view">
224
+ <span class="aok-relative s-icon-text-medium s-prime">
225
+ <i class="a-icon a-icon-prime a-icon-medium" role="img" aria-label="Amazon プライム"></i>
226
+ </span>
227
+ <span>
228
+
229
+ </span>
230
+ </span>
231
+
232
+
233
+
234
+ <span aria-label="明日中4/16に無料配送">
235
+ <span class="a-text-bold">明日中4/16</span><span>に無料配送</span>
236
+ </span>
237
+ </div></div><div class="a-row a-size-base a-color-secondary">
238
+
239
+
240
+ <span aria-label="残り2点 ご注文はお早めに">
241
+ <span class="a-color-price">残り2点 ご注文はお早めに</span>
242
+ </span>
243
+ </div>
244
+ </div>
245
+
246
+
247
+
248
+ <div class="a-section a-spacing-none a-spacing-top-mini">
249
+ <div class="a-row a-size-base a-color-secondary"><span class="a-size-base a-color-secondary">こちらからもご購入いただけます</span><br><span class="a-color-price">¥1,683</span><span class="a-letter-space"></span>
250
+
251
+
252
+
253
+
254
+ <a class="a-link-normal" href="/gp/offer-listing/B07HVKS8DM/ref=sr_1_10?rnid=2227291051&amp;s=hobby&amp;rps=1&amp;fst=as%3Aoff&amp;qid=1586877663&amp;refinements=p_n_feature_fifteen_browse-bin%3A3307612051%2Cp_76%3A2227292051&amp;sr=1-10&amp;dchild=1">
255
+
256
+
257
+
258
+ (10点の新品)
259
+
260
+
261
+ </a>
262
+ </div>
263
+ </div>
264
+
265
+
266
+ </div>
267
+ </div>
268
+
269
+ </span>
270
+
271
+ </div></div>