|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 wx486 于 2013-8-30 14:40 编辑
kangatang 发表于 2012-7-18 14:29
大师为介绍相关知识,讲得比较全面。但针对1楼的问题我给出另外一种解决思路(XMLHTTP + QURYTABLE)
受kangatang兄的启发,用了类似方法也测试成功了,没有用到解码等麻烦也不易学的代码,直接把HTTPFOX抓到POST数据粘贴生成一个文本文件(附件中的posttxt.txt),是不是可以这样理解:http://club.excelhome.net/thread-1049013-1-1.html里的第3问的答案是肯定的?。谢谢liucqa大师的教程(受益良多),谢谢kangatang兄的代码。我的代码运行稍慢,但另一个同类问题用的也是类似这个方法却很快(http://club.excelhome.net/forum. ... 5&page=1#pid7164470),速度相差这么大,不知何因?另:kangatang兄的代码在我的WIN8+OFFICE2013中运行不出错却没有取到数据。
我的代码如下:
附件:
学习网抓(懒人法).zip
(46.36 KB, 下载次数: 96)
- Sub xml()
- Dim doc As Object, i%, j%, p%, k%, s$, s1$, s2$, r
- Set doc = CreateObject("htmlfile")
- [a1].CurrentRegion.ClearContents
- t = Timer
- Application.ScreenUpdating = False
- Open ThisWorkbook.Path & "\posttxt.txt" For Input As #1
- postdata = VBA.StrConv(InputB(LOF(1), 1), 64)
- s1 = Split(postdata, "wx486")(0)
- s2 = Split(postdata, "wx486")(1)
- With CreateObject("msxml2.xmlhttp")
- For p = 1 To 20
- DoEvents: Application.StatusBar = "正在获取第" & p & "页数据,累计用时" & Format(Timer - t, "0.0秒")
- postdata = s1 & p & s2
- .Open "post", "http://www.gzgcjg.com/gzqypjtx/Estimate/ZY/MainQueryMarkZY.aspx", True
- .setRequestHeader "content-type", "application/x-www-form-urlencoded"
- .send (postdata)
- Do Until .readystate = 4
- DoEvents
- Loop
- s = .responsetext
- ' With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
- ' .SetText s
- ' .PutInClipboard
- ' End With
- doc.body.innerhtml = s
- For i = 0 To doc.all.tags("table").Length - 1
- If InStr(doc.all.tags("table")(i).innertext, "排名") Then
- Set r = doc.all.tags("table")(i).Rows
- End If
- Next
- For i = IIf(p = 1, 0, 1) To r.Length - 1
- k = k + 1
- For j = 0 To r(i).Cells.Length - 1
- Cells(k, j + 1) = r(i).Cells(j).innertext
- Next
- Next
- Next
- End With
- Close #1
- Application.ScreenUpdating = True
- MsgBox "OK,共用时" & Format(Timer - t, "0.0秒")
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|