|
楼主 |
发表于 2014-6-6 19:25
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
代码如下- Option Explicit
- Public oDoc As Object '用全局document对象避免每次调用都创建新的
- Sub 已回复帖子()
- 'On Error Resume Next
- Dim objXml As Object, i&
- Dim strText As String
- Set objXml = CreateObject("MSXML2.XMLHTTP")
- Set oDoc = CreateObject("htmlfile")
- Application.ScreenUpdating = False
- Sheet3.[A2:D65536] = ""
- i = 1
- Do
- With objXml
- .Open "GET", "http://club.excelhome.net/home.php?mod=space&uid=2617308&do=thread&view=me&type=reply&order=dateline&page=" & i & "&x=" & Rnd, False
- .send
- strText = .responseText
- End With
- GetDate strText
- i = i + 1
- If InStr(strText, "下一页") = 0 Or i = [H1] Then Exit Do '回复列表没有下一页时停止搜索下一页,为防过久运行无结果设置100页上限
- Loop
- ActiveSheet.UsedRange.RemoveDuplicates Columns:=2, Header:=xlYes '去重
- [A2] = 1
- i = [B65536].End(xlUp).Row
- [A2].AutoFill Destination:=Range("A2:A" & i), Type:=xlFillSeries '序号
- MsgBox "共为您整理回复贴" & i - 1 & "个"
- Application.ScreenUpdating = True
- Set objXml = Nothing
- Set oDoc = Nothing
- End Sub
- Public Sub GetDate(Text$)
- Dim i&, j&, txt$, k&, objTab
- Dim r, arr
- oDoc.body.innerHTML = Text$
- Set objTab = oDoc.getElementsByTagName("table").Item(3).getElementsByTagName("th")
- k = objTab.Length - 1
- ReDim arr(1 To 3, 1 To k)
- For i = 1 To k '遍历回复列表
- With objTab
- 'Debug.Print i, .Cells(1).innertex
- 'Debug.Print i, .Item(i).innerText
- arr(2, i) = .Item(i).innerText
- arr(3, i) = "http://club.excelhome.net/" & HtmlFilter(.Item(i).innerHTML, "<A href=" & """" & "about:", """")
- arr(3, i) = Replace(arr(3, i), "amp;", "") '去除无效字符
- End With
- Next
- j = [B65536].End(xlUp).Offset(1).Row
- Cells(j, 1).Resize(k, 2) = Application.Transpose(arr)
- For i = 1 To UBound(arr, 2)
- ActiveSheet.Hyperlinks.Add Cells(j + i - 1, 2), arr(3, i)
- Next
- End Sub
- Public Function HtmlFilter(ByVal htmlText$, label1$, label2$) '返回html字符串lable1和最近的lable2标签中的数据
- Dim pStart As Long, pStop As Long '开始位置,结束位置
- pStart = InStr(htmlText, label1) + Len(label1) '找到标签信息的起始位置
- If pStart <> 0 Then
- pStop = InStr(pStart, htmlText, label2)
- HtmlFilter = Mid(htmlText, pStart, pStop - pStart)
- End If
- End Function
复制代码 |
|