Dim myBy As New By
FWFlag = False
Do
FWFlag = Driver.IsElementPresent(myBy.Css(“#nav_box > div:nth-child(2) > input”)) ‘表示されるまで待つ
Driver.Wait 1000
Loop Until FWFlag = True
スクレイピング関連 VBA+seleniumbasic+Chromedriver
Dim myBy As New By
FWFlag = False
Do
FWFlag = Driver.IsElementPresent(myBy.Css(“#nav_box > div:nth-child(2) > input”)) ‘表示されるまで待つ
Driver.Wait 1000
Loop Until FWFlag = True
Public Sub Sample()
MsgBox EncodeURL(“キーワード”)
End Sub
Private Function EncodeURL(ByVal sWord As String) As String
Dim d As Object
Dim elm As Object
sWord = Replace(sWord, “\”, “\”)
sWord = Replace(sWord, “‘”, “\'”)
Set d = CreateObject(“htmlfile”)
Set elm = d.createElement(“span”)
elm.setAttribute “id”, “result”
d.body.appendChild elm
d.parentWindow.execScript “document.getElementById(‘result’).innerText = encodeURIComponent(‘” & sWord & “‘);”, “JScript”
EncodeURL = elm.innerText
End Function
For k = 2 To row01 ‘画像アドレス取得
chk = Cells(k, 4).Value ’画像を探すページアドレスの一覧
driver.Get (chk)
Set elements = driver.FindElementsByTag("img") ’ページから imgタグのある要素一気に抜き出し
For i = 1 To elements.Count
'リンクURLを収集する
aLink = elements.Item(i).Attribute("src")
‘ atitl = elements.Item(i).Text
If InStr(aLink, "https://m.media-amazon.com") <> 0 And InStr(aLink, "UL320") <> 0 Then ’抜き出された画像ドレスに色々条件つけて確定
sh01.Cells(k, 5).Value = aLink
Exit For
End If
Next i
’Chromeを動かし始めた時に邪魔なので画面サイズを半分にして右端に寄せる
Private Declare Function GetSystemMetrics _
Lib “user32” _
(ByVal nIndex As Long) As Long
Private Const SM_CXSCREEN As Long = 0
Private Const SM_CYSCREEN As Long = 1
myX = GetSystemMetrics(SM_CXSCREEN) '画面の幅を取得
myY = GetSystemMetrics(SM_CYSCREEN) '画面の高さを取得
myX = myX / 2 '画面の幅を半分
myY = myY - 50
Driver.Window.SetPosition myX, 20 '画面の幅を半分の位置(右側にウインドウを移動)
Driver.Window.SetSize myX, myY 'ウインドウの高さは-50にして幅は半分