Option Explicit
Public Sub GetHtmlTitle()
Sheets("Sheet1").Select
Range("A1").Select
Do Until ActiveCell.Value = ""
Dim url As String
Dim html As String
url = ActiveCell.Value
html = GetHttpResponse("GET", url, "", "_autodetect")
ActiveCell.Offset(0, 1).Value = ExtractTitle(html)
ActiveCell.Offset(1, 0).Activate
Loop
MsgBox "おしまい"
End Sub
Private Function GetHttpResponse(ByVal methodType, ByVal url, ByVal sendData, ByVal charset)
Const adTypeBinary = 1
Const adTypeText = 2
Const agent As String = "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1)"
Dim dst
Set dst = CreateObject("ADODB.Stream")
With dst
.Open
.Type = adTypeBinary
'HttpRequest
Dim xmlhttp
Set xmlhttp = CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open methodType, url, False
xmlhttp.SetRequestHeader "User-Agent", agent
xmlhttp.send (sendData)
.Write (xmlhttp.responseBody) 'ADODB.Streamオブジェクトへ書き込み
.Position = 0
.Type = adTypeText
.charset = charset
GetHttpResponse = .ReadText 'Textとして読み出す
.Close
End With
Set dst = Nothing
End Function
Private Function ExtractTitle(ByVal html As String)
'正規表現オブジェクト
Dim oRe, oMatch, oMatches
'正規表現オブジェクトの参照をセット(新規作成)
Set oRe = CreateObject("VBScript.RegExp")
'正規表現オブジェクトのオプション設定
With oRe
.Global = True
.IgnoreCase = True
End With
'マッチパターン作成
oRe.Pattern = "title[\s]*>([^<>/]+)<[\s]*/title"
'正規表現実行
Set oMatches = oRe.Execute(html)
'マッチした全てに対して処理する
For Each oMatch In oMatches
Set oMatches = oMatch.SubMatches
If oMatches.Count = 1 Then
ExtractTitle = oMatches.Item(0) 'oMatch.Value
Exit For
Else
ExtractTitle = "おかしい・・・"
Exit For
End If
Next
End Function