ウェブページからソース全体を保存する場合と、htmlタグを除いたテキスト部分のみを保存する場合のサンプルです。
テキスト部分のみを取り出すときは、htmlのタグからタグまでのコードを除去する関数を追加しています。
|
|
Public Sub Sample_1()
Dim TargetURL As String
Dim xmlHttp As Object
Dim HtmlSource As String
Dim n As Long
TargetURL = "https://www.yahoo.co.jp/"
Set xmlHttp = CreateObject("Msxml2.XMLHTTP")
xmlHttp.Open "GET", TargetURL, False
xmlHttp.Send
HtmlSource = xmlHttp.ResponseText
Set xmlHttp = Nothing
n = FreeFile
Open ThisWorkbook.Path & "\test1.txt" For Output As #n
Print #n, HtmlSource
Close #n
End Sub
|
|
Public Sub Sample_2()
Dim TargetURL As String
Dim xmlHttp As Object
Dim TextSource As String
Dim n As Long
TargetURL = "https://www.yahoo.co.jp/"
Set xmlHttp = CreateObject("Msxml2.XMLHTTP")
xmlHttp.Open "GET", TargetURL, False
xmlHttp.Send
TextSource = GetText(xmlHttp.ResponseText)
Set xmlHttp = Nothing
n = FreeFile
Open ThisWorkbook.Path & "\test2.txt" For Output As #n
Print #n, TextSource
Close #n
End Sub
|
|
Public Function GetText(mySource As String) As String
Dim i As Long
Dim myText As String
Dim chr As String
Dim bTag As Boolean
For i = 1 To Len(mySource)
chr = Mid(mySource, i, 1)
If chr = "<" Then bTag = True 'タグの開始
If Not bTag Then
myText = myText & chr
End If
If chr = ">" Then bTag = False 'タグの終了
Next i
GetText = myText
End Function
|