3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBScriptによるInternetExplorerクローラー化サンプル

Posted at

#概要
InternetExplorerをVBScript経由でクローラー化するサンプルを記述する。
Google検索を自動で行い、HTMLを取得する。
職場でも安心バッチ実行できるね。

#コード

IE.bat
CScript IE.vbs > LOG.txt
IE.vbs
Option Explicit
 
Private Const URL_TARGET = "http://google.co.jp/"
Private Const TIME_MINIMUMWAIT = 1500
Private Const TIME_MAXWAIT = 3000
 
Main
 
Public Sub Main()
    Dim ie
    Dim doc
    
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    
    IENavigate ie, URL_TARGET
    
    Set doc = ie.document
    With doc
        .getElementById("gbqfq").Value = "google"
        .getElementById("gbqfba").Click
    End With
    IEWaitReady ie
    
    WScript.echo doc.Body.InnerHtml
    
    ie.Quit
    
    Set doc = Nothing
    Set ie = Nothing
End Sub
 
Public Sub IENavigate(ByRef ie, ByVal url)
    ie.navigate url
    IEWaitReady ie
    IEWaitRandom
End Sub
 
Public Sub IEWaitReady(ByRef ie)
    Do While ie.Busy = True Or ie.readyState <> 4
        WScript.Sleep 100
    Loop
    IEWaitRandom
End Sub
 
Public Sub IEWaitRandom()
    Dim milliseconds
    milliseconds = -1
    Do While milliseconds < TIME_MINIMUMWAIT
        milliseconds = Int(Rnd * TIME_MAXWAIT)
    Loop
    WScript.Sleep milliseconds
End Sub
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?