ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 如何使用多种方式提取多个网页的数据

如何使用多种方式提取多个网页的数据

作者:绿色风 分类: 时间:2022-08-18 浏览:96
楼主
xmyjk
Q:从


A:第一种方法QUERYTABLE:
  1. Sub 宏1()
  2.   Dim Y&, M&, D&, arr, n, nm
  3.   
  4.   ActiveSheet.UsedRange.Clear
  5.   arr = Array("Beijing", "Shanghai")
  6.   
  7.   For n = LBound(arr) To UBound(arr)
  8.   
  9.   For i = 0 To 2
  10.   Y = 1 '调整变量Y(对应年),M(对应月),D(对应日)的不同值,即可得到不同日期的结果
  11.   M = 0 '本例为1年后(即2012-6-4日的天气情况)
  12.   D = i
  13.   
  14.   With ActiveSheet.QueryTables.Add(Connection:= _
  15.   "URL;http://www.t7online.com/t7text/" & arr(n) & "/wtable" & Format(DateSerial(Year(Date) + Y, Month(Date) + M, Day(Date) + D), "yyyymmdd") & ".htm", Destination:=Cells(n * 50 + 1, i * 10 + 1))
  16.   .BackgroundQuery = True
  17.   .RefreshStyle = xlOverwriteCells
  18.   .AdjustColumnWidth = True
  19.   .WebSelectionType = xlSpecifiedTables
  20.   .WebFormatting = xlWebFormattingNone
  21.   .WebTables = "6"
  22.   .Refresh BackgroundQuery:=False
  23.   End With
  24.    
  25.   Cells(n * 50 + 1, i * 10 + 1).QueryTable.Delete
  26.    
  27.   Next
  28.   
  29.   Next
  30.   
  31. End Sub


第二种方法:XMLHTTP:
  1. Option Explicit
  2. Sub test()
  3. Dim tmp() As String, i As Integer, arr() As String, xmlhttp As Object, N As Long, tmp1() As String, riqi As String
  4. Dim Y As Integer, M As Integer, D As Integer, CITY, P As Integer, X As Integer
  5.                
  6. ActiveSheet.UsedRange.Clear
  7. CITY = Array("Beijing", "Shanghai")
  8.    
  9. For P = LBound(CITY) To UBound(CITY)
  10.   For X = 0 To 2
  11.     Y = 1: M = 0: D = X
  12.    
  13.     Cells(P * 50 + 1, X * 10 + 1).Offset(1).Resize(1, 10) = Split("地市州,夜间,白天,最高气温,最高气温趋势,最低气温,白天风向,白天风速,21日降水量,紫外线指数", ",")
  14.    
  15.     Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
  16.     With xmlhttp
  17.         .Open "get", "http://www.t7online.com/t7text/" & CITY(P) & "/wtable" & Format(DateSerial(Year(Date) + Y, Month(Date) + M, Day(Date) + D), "yyyymmdd") & ".htm", False
  18.         .send
  19.         tmp = Filter(Split(Replace(Replace(Replace(Replace(Replace(Replace(.responsetext, """middle""><", """middle""> <"), ";""><", ";""> <"), """20""><", """20"""), """><", "<"), """ TITLE=""", ">"), "</a>", ""), "</td>"), "<td height=""20""")
  20.         riqi = Trim(Split(Split(Replace(.responsetext, vbLf, ""), "<td align=""center"" style=""font-weight: bold;"">")(1), "&")(0))
  21.     End With
  22.    
  23.     ReDim arr(UBound(tmp) \ 10, 9)
  24.     For i = 0 To UBound(tmp)
  25.         tmp1() = Split(tmp(i), ">")
  26.         arr(i \ 10, i Mod 10) = Trim(tmp1(UBound(tmp1)))
  27.     Next
  28.    
  29.     Cells(P * 50 + 1, X * 10 + 1) = riqi
  30.     Cells(P * 50 + 1, X * 10 + 1).Offset(2).Resize(UBound(arr) + 1, UBound(arr, 2) + 1) = arr
  31.    
  32.     Erase tmp, tmp1
  33.     Erase arr
  34.     Set xmlhttp = Nothing
  35.   Next
  36. Next

  37. [a:AD].Columns.AutoFit

  38. MsgBox "Ok"
  39. End Sub


天气预报XMLHTTP版本.rar
天气预报查询querytable.rar
2楼
清华
太棒了 
3楼
huangzhihua
下載謝謝学习,,,,,,,,,,,,,,,,,,,,
4楼
eliane_lei
谢谢分享!

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap