ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: zhczhy2016

急,VBA多个网页抓取数据

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-12-14 15:01 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
zhczhy2016 发表于 2016-12-14 14:54
这个不会提示内存溢出,这个是是对提取后的结果进行了检查,把重复的项去除,是吗?!

是的 你试试看

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-12-15 14:14 | 显示全部楼层

测试过,效果不错,非常感谢,不过又遇到了新的问题:如何从ASP网页中提取数据。这个网址就是ASP,不知怎么办了?
http://59.49.34.87:2016/Defalut_1.aspx
其中的“补贴情况公示”,打开没有网址,不知怎么办了?

TA的精华主题

TA的得分主题

发表于 2016-12-15 17:40 | 显示全部楼层
zhczhy2016 发表于 2016-12-15 14:14
测试过,效果不错,非常感谢,不过又遇到了新的问题:如何从ASP网页中提取数据。这个网址就是ASP,不知怎 ...

试试看
  1.     Dim strText As String, t, i&, j&, TR, TD, n&
  2.     Dim reg, n_max&, viewstate$
  3.     Set reg = CreateObject("vbscript.regexp")
  4.     reg.Global = True
  5.     reg.Pattern = "共(\d+)页"
  6.     Application.ScreenUpdating = False
  7.     Sheet1.Cells.ClearContents
  8.     With CreateObject("WinHttp.WinHttpRequest.5.1")
  9.         Do
  10.             n = n + 1
  11.             .Open "POST", "http://59.49.34.87:2016/ApplicationProcess/HuiZTJ_ZJSY.aspx", False
  12.             .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  13.             If n = 1 Then
  14.                 .Send "GridView1%24ctl34%24txtGoPage=1"
  15.             Else
  16.                 .Send "__VIEWSTATE=" & viewstate & "&GridView1%24ctl34%24txtGoPage=" & n
  17.             End If
  18.             strText = .responsetext
  19.             With CreateObject("htmlfile")
  20.                 .write strText
  21.                 viewstate = encodeURI(.getElementById("__VIEWSTATE").Value)
  22.                 t = 0
  23.                 For Each TR In .all.tags("table")("GridView1").Rows
  24.                     t = t + 1
  25.                     If ((t = 1 Or t = 2) And n = 1) Or t > 2 Then
  26.                         i = i + 1: j = 0
  27.                         For Each TD In TR.Cells
  28.                             If n = 1 Then
  29.                                 If reg.test(TD.innertext) Then
  30.                                     n_max = reg.Execute(TD.innertext)(0).submatches(0)
  31.                                 End If
  32.                             End If
  33.                             If reg.test(TD.innertext) Then i = i - 1: Exit For
  34.                             j = j + 1
  35.                             Sheet1.Cells(i, j) = TD.innertext
  36.                         Next
  37.                     End If
  38.                 Next
  39.             End With
  40.             If n = n_max Then Exit Do
  41.         Loop
  42.     End With
  43.     Application.ScreenUpdating = True
  44. End Sub
  45. Function encodeURI(strTobecoded As String) As String
  46.     With CreateObject("msscriptcontrol.scriptcontrol")
  47.         .Language = "JavaScript"
  48.         encodeURI = .Eval("encodeURIComponent('" & strTobecoded & "');")
  49.         'encodeURIComponent无法转换括号,所以再替换下括号
  50.         encodeURI = Replace(Replace(encodeURI, "(", "%28"), ")", "%29")
  51.     End With
  52. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2016-12-15 18:07 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
zhczhy2016 发表于 2016-12-15 14:14
测试过,效果不错,非常感谢,不过又遇到了新的问题:如何从ASP网页中提取数据。这个网址就是ASP,不知怎 ...

n_max指定页数 不指定全部下载
  1. Sub tet()
  2.     Dim strText As String, t, i&, j&, TR, TD, n&
  3.     Dim reg, n_max&, viewstate$
  4.     n_max = 10
  5.     Set reg = CreateObject("vbscript.regexp")
  6.     reg.Global = True
  7.     reg.Pattern = "共(\d+)页"
  8.     Application.ScreenUpdating = False
  9.     Sheet1.Cells.ClearContents
  10.     Sheet1.Range("A:A").NumberFormatLocal = "@"
  11.     With CreateObject("WinHttp.WinHttpRequest.5.1")
  12.         Do
  13.             n = n + 1
  14.             .Open "POST", "http://59.49.34.87:2016/Application/gongs.aspx", False
  15.             .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  16.             If n = 1 Then
  17.                 .Send "GridView1%24ctl53%24txtGoPage=1"
  18.             Else
  19.                 .Send "__VIEWSTATE=" & viewstate & "&GridView1%24ctl53%24txtGoPage=" & n
  20.             End If
  21.             strText = .responsetext
  22.             With CreateObject("htmlfile")
  23.                 .write strText
  24.                 viewstate = encodeURI(.getElementById("__VIEWSTATE").Value)
  25.                 t = 0
  26.                 For Each TR In .all.tags("table")("GridView1").Rows
  27.                     t = t + 1
  28.                     If (t = 1 And n = 1) Or t > 1 Then
  29.                         i = i + 1: j = 0
  30.                         For Each TD In TR.Cells
  31.                             If n = 1 Then
  32.                                 If reg.test(TD.innertext) Then
  33.                                     n_max = IIf(Val(reg.Execute(TD.innertext)(0).submatches(0)) > n_max, n_max, Val(reg.Execute(TD.innertext)(0).submatches(0)))
  34.                                 End If
  35.                             End If
  36.                             If reg.test(TD.innertext) Then i = i - 1: Exit For
  37.                             j = j + 1
  38.                             Sheet1.Cells(i, j) = TD.innertext
  39.                         Next
  40.                     End If
  41.                 Next
  42.             End With
  43.             If n = n_max Then Exit Do
  44.         Loop
  45.     End With
  46.     Application.ScreenUpdating = True
  47. End Sub
  48. Function encodeURI(strTobecoded As String) As String
  49.     With CreateObject("msscriptcontrol.scriptcontrol")
  50.         .Language = "JavaScript"
  51.         encodeURI = .Eval("encodeURIComponent('" & strTobecoded & "');")
  52.         'encodeURIComponent无法转换括号,所以再替换下括号
  53.         encodeURI = Replace(Replace(encodeURI, "(", "%28"), ")", "%29")
  54.     End With
  55. End Function
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-12-16 08:17 | 显示全部楼层
yangyangzhifeng 发表于 2016-12-15 18:07
n_max指定页数 不指定全部下载

你真牛,以后有空好好研究一下你的编码,到时还得请教你

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-12-16 16:39 | 显示全部楼层

这个网页数据,每次打开也会有不同的变动,是不是也需在除重?

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-10 08:14 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
yangyangzhifeng 发表于 2016-12-15 18:07
n_max指定页数 不指定全部下载

这个可以分页提取吗?一次性全部提取,时间太长,网站提示“time out‘

TA的精华主题

TA的得分主题

发表于 2017-1-12 20:46 | 显示全部楼层
  1. Sub test()
  2. Dim ie, dmt, tb, x, y, i
  3.     Set ie = CreateObject("InternetExplorer.Application")
  4.     With ie
  5.         .Visible = True '显示它
  6.         .navigate "http://125.70.15.76:2016/pub/gongshi?pageIndex=1" '加载某个页面
  7.         Do Until .ReadyState = 4 And .busy = False '等待页面加载完毕
  8.             DoEvents
  9.         Loop
  10.         Set dmt = .document
  11.         Debug.Print dmt.body.outerhtml
  12.         'Stop
  13.         For i = 1 To 10
  14.         
  15.         
  16.         Set dmt = .document
  17.         Set tb = dmt.all.tags("table")(1)
  18.         For x = 0 To tb.Rows.Length - 1
  19.             For y = 0 To tb.Rows(x).Cells.Length - 1
  20.                 Cells(x + 1, y + 1) = tb.Rows(x).Cells(y).innertext
  21.             Next y
  22.         Next x
  23.         dmt.getElementById("_MvcPager_Ctrl0_pib").Value = i + 1
  24.         dmt.getElementsByTagName("input")(7).Click
  25.         
  26.         Do Until .ReadyState = 4 And .busy = False '等待页面加载完毕
  27.             Debug.Print .busy
  28.             DoEvents
  29.         Loop
  30.         'Stop
  31.         Worksheets.Add after:=Worksheets(Worksheets.Count)
  32.         Next i
  33.         
  34.     End With
  35. End Sub

复制代码

TA的精华主题

TA的得分主题

发表于 2017-1-18 09:11 | 显示全部楼层
http://59.49.34.87:2016/Application/gongs.aspx该网址数据较多,代码运行时间较长。

kaohsing_2.zip

212 KB, 下载次数: 59

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-7 10:07 | 显示全部楼层
Kaohsing 发表于 2017-1-18 09:11
http://59.49.34.87:2016/Application/gongs.aspx该网址数据较多,代码运行时间较长。

非常感谢,
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-24 14:34 , Processed in 0.045054 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表