selectタグのoptionタグ要素をスクレイピングしselectedする。
【HTML】
'<select name="rank1CodeMulti" class="code_assist_select" id="ID_rank1CodeMulti" size="12" multiple="">
'<option value="01101">あ</option>
'<option value="01102">い</option>
'<option value="01103">う</option>
'</select>
【VBA】
key = ”い”
i = 0
For Each objINPUT In ie.document.GetElementsByName("rank1CodeMulti")(0).All
If objINPUT.tagname = "OPTION" Then
If key = objINPUT.outertext Then Exit For
i = i + 1
End If
Next
ie.document.All("rank1CodeMulti")(i).Selected = True
<select>タグの<option>タグで Selectedされているtextをスクレイピングするコード
【VBA】
For Each objINPUT In ie.document.getElementsByName("tDFK1CmbBox")(0).all '''''''''スクレイピング
If objINPUT.tagname = "OPTION" Then
If objINPUT.Selected Then
keyword.Range("B4") = objINPUT.outerText '''''スクレイピング
Exit For
End If
End If
Next
【HTML例】
'<select name="tDFK1CmbBox" id="ID_tDFK1CmbBox">
'<option value="" selected=""></option>
'<option value="01">北海道</option>
省略
<input>タグのcheckedをスクレイピングするコード 以下のようなHTMLでcheckedされたtext(以下の例では一般求人)
【VBA】
For Each objINPUT In ie.document.all.tags("label")
If InStr(objINPUT.outerHTML, "ID_LkjKbnRadioBtn1") > 0 Then
If InStr(objINPUT.outerHTML, "checked") > 0 Then
keyword.Range("B2") = objINPUT.outerText '''''スクレイピング"
End If
End of
Next
【HTML例】ハローワークHPから '求人区分 '<label id="ID_LkjKbnRadioBtn1" for="ID_kjKbnRadioBtn1"><input name="kjKbnRadioBtn" id="ID_kjKbnRadioBtn1" type="radio" checked="" value="1">一般求人</label> '<label id="ID_LippanCKBox1" for="ID_ippanCKBox1"><input name="ippanCKBox" id="ID_ippanCKBox1" type="checkbox" value="1">フルタイム</label> '<label id="ID_LippanCKBox2" for="ID_ippanCKBox2"><input name="ippanCKBox" id="ID_ippanCKBox2" type="checkbox" value="2">パート</label> '<label id="ID_LkjKbnRadioBtn2" for="ID_kjKbnRadioBtn2"><input name="kjKbnRadioBtn" id="ID_kjKbnRadioBtn2" type="radio" value="2">新卒・既卒求人</label> '<label id="ID_LkjKbnRadioBtn3" for="ID_kjKbnRadioBtn3"><input name="kjKbnRadioBtn" id="ID_kjKbnRadioBtn3" type="radio" value="3">季節求人</label> '<label id="ID_LkjKbnRadioBtn4" for="ID_kjKbnRadioBtn4"><input name="kjKbnRadioBtn" id="ID_kjKbnRadioBtn4" type="radio" value="4">出稼ぎ求人</label> '<label id="ID_LkjKbnRadioBtn5" for="ID_kjKbnRadioBtn5"><input name="kjKbnRadioBtn" id="ID_kjKbnRadioBtn5" type="radio" value="5">障害のある方のための求人</label> '<label id="ID_LsGSYACKBox1" for="ID_sGSYACKBox1"><input name="sGSYACKBox" disabled="" id="ID_sGSYACKBox1" type="checkbox" value="1">フルタイム</label> '<label id="ID_LsGSYACKBox2" for="ID_sGSYACKBox2"><input name="sGSYACKBox" disabled="" id="ID_sGSYACKBox2" type="checkbox" value="2">パート</label>
VBAでWebスクレイピングに必須機能がIEに標準で付いている。
詳細は以下で確認出来る。
https://beginners-hp.com/developers_tool.html
スクレイピングしたい所にカーソルを当てて機能を使うと
【例】
【実例1】
Google画面の検索フォームのHTML要素が見える。
ファームの名前が”q”とわかる。
以下のコードで検索キーワードがファームに入力される。
Set objINPUT = ie.Document.getElementsByName("q")(0)’’’’’’’’’’複数同名の場合 0が変わる
objINPUT.Value = ”キーワード” '''''検索キーワード入力
waittime = Now + TimeValue("0:00:03")
Application.Wait waittime
Set objINPUT = Nothing ''''''''''必須(書き込みエラー)
【実例2】
Google画面の検索ボタンのHTML要素が見える。
ボタンの名前が”btnK”とわかる。
以下のコードでボタンが自動でクリックされる。
For Each objINPUT In ie.Document.All.tags("input")
If objINPUT.Name = "btnK" Then
objINPUT.Click '''''''''検索ボタンを押す
Exit For
End If
Next
無料翻訳のWebアプリをアシストするVBAを作ってみた。
こんな感じ。自動操作で5言語をOutPut。
URL = "https://www.excite.co.jp/world/" '''''''URL設定
With ie
.navigate (URL)
.Visible = True
End With
DocumentTitle = "エキサイト 翻訳" '''''''''''HTMLタイトル設定
Do
Set ie = getIE(DocumentTitle) '''''''画面が出力するまで待機(繰り返す)
If ie Is Nothing Then
Else
Exit Do
End If
Loop
For i = 0 To 4
If i = 0 Then
ii = 4
Else
If i = 1 Then
ii = 6
言語 = "/world/chinese/"
言語x = "before_ch_kn"
End If
If i = 2 Then
ii = 8
言語 = "/world/korean/"
言語x = "before_ko"
End If
If i = 3 Then
ii = 10
言語 = "/world/french/"
言語x = "before_fr"
End If
If i = 4 Then
ii = 12
言語 = "/world/german/"
言語x = "before_de"
End If
For Each objINPUT In ie.Document.all.tags("a") '''''''''言語ボタンを押す
If InStr(objINPUT.outerHTML, 言語) > 0 Then
objINPUT.Click
Exit For
End If
Next
Set objINPUT = Nothing
waittime = Now + TimeValue("0:00:03")
Application.Wait waittime
go = ""
Do
For Each objINPUT In ie.Document.all.tags("a")
If InStr(objINPUT.outerHTML, 言語x) > 0 Then '''''''画面が出力するまで待機(繰り返す) go = "go"
End If
Next
If go = "go" Then Exit Do
Loop
End If
For Each objINPUT In ie.Document.all.tags("a") '''''''''日ボタンを押す
If InStr(objINPUT.outerHTML, "before_ja") > 0 Then
objINPUT.Click
Exit For
End If
Next
waittime = Now + TimeValue("0:00:03")
Application.Wait waittime
key = 翻訳.Range("A2").Value
Set objInpTxt = ie.Document.getElementsByName("before")(0) objInpTxt.Value = key '''''検索キーワード入力
For Each objINPUT In ie.Document.all.tags("input") '''''''''翻訳ボタンを押す
If InStr(objINPUT.outerHTML, "exec_transfer") > 0 Then
objINPUT.Click
Exit For
End If
Next
waittime = Now + TimeValue("0:00:03")
Application.Wait waittime
go = ""
Do
For Each objINPUT In ie.Document.all.tags("textarea") '''''''''スクレイピング
If InStr(objINPUT.outerHTML, "after") > 0 Then
翻訳.Range("A" & ii).Value = objINPUT.outertext
If 翻訳.Range("A" & ii).Value = "" Then go = ""
If 翻訳.Range("A" & ii).Value <> "" Then go = "go"
Exit For
End If
Next
Set objINPUT = Nothing
If go = "go" Then Exit Do
Loop
Next i