Const cnsDTop As String = "<DIV class=listDiaryTitle>" Const cnsCTop As String = "<DL class=comment>"
Dim OwnerId As String: OwnerId = "399831" 'My Mixi User ID If OwnerId = "" Then OwnerId = MsgBox("MIXI ID?") If OwnerId = "" Then Exit Sub End If End If Dim DiaryId As String: DiaryId = "" 'Start(最新) Diary ID If DiaryId = "" Then DiaryId = MsgBox("日記ID?") If DiaryId = "" Then Exit Sub End If End If Dim NextId As String: NextId = "" Dim stURL As String: Dim s As String, buf, i As Integer Dim sE As String, bufE, k As Integer Dim stGyo As String Dim ie As Object Dim j As Long
On Error Resume Next Set ie = CreateObject("InternetExplorer.Application")
' ダウンロード待ち Do While ie.Busy Loop ' Sheets("Sheet1").Select ' Range("A1").Select If MsgBox("この日記をセーブしますか?", vbOKCancel + vbMsgBoxSetForeground) = vbCancel Then GoTo END_PROC End If
' 日記取得(閲覧ページ) s = ie.document.body.innerHTML buf = Split(s, vbCr)
Open "C:\Users\Public\Desktop\D" & DiaryId & ".txt" For Output As #1 For i = 0 To UBound(buf) ' 前の日記のid取得 If InStr(buf(i), "前の日記") > 0 Then stGyo = Mid(buf(i), InStr(buf(i), "前の日記") - 15, 13) NextId = "" For j = 1 To 13 If IsNumeric(Mid(stGyo, j, 1)) Then NextId = NextId & Mid(stGyo, j, 1) End If Next j End If
' 日記本文出力 stGyo = Mid(buf(i), 2, Len(cnsDTop) + 1) If UCase(stGyo) = UCase(cnsDTop) Then i = i + 2 stGyo = Mid(buf(i), 6, InStr(buf(i), "<SPAN>") - 6) ' タイトル stGyo = ImgEdit(stGyo) Print #1, stGyo i = i + 1 stGyo = Mid(buf(i), 6, InStr(buf(i), "</DD>") - 6) ' 投稿日時 Print #1, stGyo i = i + 2
' 本文は編集ページ(退避)より取得 For k = 50 To UBound(bufE) If InStr(bufE(k), "diaryBody") > 0 Then stGyo = Mid(bufE(k), InStr(bufE(k), "diaryBody")) stGyo = Mid(stGyo, InStr(stGyo, ">") + 1) Print #1, stGyo k = k + 1 Do While k < UBound(bufE) stGyo = bufE(k) If InStr(stGyo, "</TEXTAREA>") > 0 Then stGyo = Left(stGyo, InStr(stGyo, "</TEXTAREA>") - 1) k = UBound(bufE) 'FINAL ElseIf InStr(stGyo, "</div>") > 0 Then stGyo = Left(stGyo, InStr(stGyo, "</div>") - 1) k = UBound(bufE) 'FINAL Else k = k + 1 End If stGyo = Replace(stGyo, vbCr, "") stGyo = Replace(stGyo, vbLf, "") Print #1, stGyo Loop End If Next k End If
' コメント出力 stGyo = Mid(buf(i), 2, Len(cnsCTop) + 1) If UCase(stGyo) = UCase(cnsCTop) Then Print #1, "=========*=========*=========*=========*=========*=========*" i = i + 1 stGyo = Mid(buf(i), InStr(buf(i), "id="), InStr(buf(i), "</SPAN>")) stGyo = ImgEdit(stGyo) Print #1, stGyo i = i + 1 stGyo = Mid(buf(i), 5, InStr(buf(i), "</DD>")) stGyo = ImgEdit(stGyo) Print #1, stGyo End If Next i
Close #1 If NextId = "" Or DiaryId = NextId Then stURL = "http://mixi.jp/neighbor_diary.pl?id=" & DiaryId & "&owner_id=" & OwnerId & "&direction=prev" ie.Visible = True ie.Navigate (stURL) ' ダウンロード待ち Do While ie.Busy Loop edit_diary.pl s = ie.document.body.innerHTML buf = Split(s, vbCr) NextId = "" For i = 80 To UBound(buf) ' 前の日記のid取得 If InStr(buf(i), "編集する") > 0 Then stGyo = Mid(buf(i), InStr(buf(i), "編集する") - 15, 13) For j = 1 To 13 If IsNumeric(Mid(stGyo, j, 1)) Then NextId = NextId & Mid(stGyo, j, 1) End If Next j End If Next i End If DiaryId = NextId Loop
Set ie = Nothing
END_PROC:
MsgBox "ウェブページ取得処理終了"
End Sub
' タグ等除去 Private Function ImgEdit(inGyo As String) As String
If InStr(inGyo, "<IMG") > 0 Then Dim i As Long: i = 1 Do While i <= Len(inGyo) If Mid(inGyo, i, 4) = "<IMG" Then i = i + 5 Do While i <= Len(inGyo) And Mid(inGyo, i, 1) <> ">" i = i + 1 Loop Else otGyo = otGyo & Mid(inGyo, i, 1) i = i + 1 End If Loop Else otGyo = inGyo End If
' トピック検索
For Each TagDt In ie.Document.All.Tags("DT")
If TagDt.GetAttribute("class") = "bbsTitle clearfix" Then
Worksheets(1).Cells(i, 1).Value = i - 1 ' No.
For Each TagA In TagDt.All.Tags("A")
If TagA.GetAttribute("class") = "title" Then
stTitle = TagA.InnerText ' タイトル
stURL = TagA.Href
Exit For
End If
Next
BbsId = Right(stURL, Len(stURL) - InStr(stURL, "&id=") - 3) ' BBS ID
For Each TagSPAN In TagDt.All.Tags("SPAN")
If TagSPAN.GetAttribute("class") = "date" Then
stDateAndTime = TagSPAN.InnerText ' 投稿日時
Exit For
End If
Next
Worksheets(1).Cells(i, 2).Select
ActiveSheet.Hyperlinks.Add anchor:=Selection, _
Address:=stURL, TextToDisplay:=stTitle
Worksheets(1).Cells(i, 3).Value = BbsId
Worksheets(1).Cells(i, 5).Value = stDateAndTime
ElseIf TagDt.GetAttribute("class") = "commentNumber" Then
For Each TagA In TagDt.All.Tags("A")
CommSu = TagA.InnerText ' コメント数
Exit For
Next
Worksheets(1).Cells(i, 4).Value = CommSu
i = i + 1
End If
Next
もっと単純化して
' トピック検索
For Each TagDt In ie.Document.All.Tags("DT")
If TagDt.GetAttribute("class") = "bbsTitle clearfix" Then
Worksheets(1).Cells(i, 1).Value = i - 1 ' No.
With TagDt.All.Tags("A")(0)
stTitle = .InnerText ' タイトル
stURL = .Href
End With
BbsId = Right(stURL, Len(stURL) - InStr(stURL, "&id=") - 3) ' BBS ID
stDateAndTime = TagDt.All.Tags("SPAN")(1).InnerText ' 投稿日時
Worksheets(1).Cells(i, 2).Select
ActiveSheet.Hyperlinks.Add anchor:=Selection, _
Address:=stURL, TextToDisplay:=stTitle
Worksheets(1).Cells(i, 3).Value = BbsId
Worksheets(1).Cells(i, 5).Value = stDateAndTime
ElseIf TagDt.GetAttribute("class") = "commentNumber" Then
CommSu = TagDt.All.Tags("A")(0).InnerText ' コメント数
Worksheets(1).Cells(i, 4).Value = CommSu
i = i + 1
End If
Next