とりあえずEXCEL(VBA)からHTTPにアクセスしてソースを持ってきてみる


現在の課題としては…・・・


1.事前にサイトの文字コードを調べたいけど方法が不明(現在は呼び出し元で決めうち)
2.サイトの存在、サイトURLのチェックを入れるべきか?(やるとすればAPIで対応予定)
3.『MSXML2.XMLHTTP』を使いたいけど .send()でno-chaseを送信しても無視される?謎


アメブロは色つけるのがメンドイ気がする

慣れてないだけかな


---------------------------------------------------------------------------
'機能:HTMLデータの取得
'備考:センス悪い
'作成:2010/08/27
'更新:
'---------------------------------------------------------------------------

Public Function ReadHtmlSource(ByVal pUrl As String, _
ByVal pOption As String, _
ByRef pSource As String, _
Optional ByVal pSJIS As Boolean) As Boolean

'Get Ready(^ω^)ノ
Dim ohttp As Object
Set ohttp = CreateObject("Microsoft.XMLHTTP")

'Set ohttp = CreateObject("MSXML2.XMLHTTP") 'Sendの方法が違う?

Dim cnt As Integer

'オブジェクトの作成チェック
If ohttp Is Nothing Then
MsgBox "『MSXML2.XMLHTTP』オブジェクトの作成に失敗しました。", vbCritical
Exit Function
End If


'HTTPリクエスト
ohttp.Open "GET", pUrl, False '同期通信(False)
ohttp.Send (pOption)


'取得結果チェック...レベル低いな(汗

While (ohttp.readystate <> 4)


If cnt > 5 Then
MsgBox "『HTMLデータ』の取得に失敗(未完了)しました。", vbCritical
Exit Function
End If


Application.Wait [NOW()+"0:00:00.5"]
cnt = cnt + 1


Wend


If ohttp.Status < 200 Or ohttp.Status >= 300 Then

MsgBox "『HTMLデータ』の取得に失敗(サイトエラー)しました。", vbCritical
Exit Function

End If


'ソースの取得

If pSJIS = True Then
pSource = StrConv(ohttp.responseBody, vbUnicode)
Else
pSource = ohttp.responseText
End If


'Release
Set ohttp = Nothing


'Return
ReadHtmlSource = True

End Function