ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何提取网页源文件中两个指定字符串之间的内容呢?

如何提取网页源文件中两个指定字符串之间的内容呢?

作者:绿色风 分类: 时间:2022-08-17 浏览:90
楼主
0Mouse
Q:如何提取网页源文件中两个指定字符串之间的内容呢?
源文件中首字符串:

 
源文件中尾字符串:

 
A:思路:借助Split函数先后以首、尾字符串为分隔符拆分成数组后提取。
示例代码如下:
  1. Sub 提取两个指定字符串之间的内容()
  2.     Dim f$, sr$, jg$
  3.     f = ThisWorkbook.Path & "\"
  4.     Open f & "sample.txt" For Input As #1
  5.     Do While Not EOF(1)
  6.         sr = sr & Input(1, #1)
  7.     Loop
  8.     Close #1
  9.     Open f & "\jg.txt" For Output As #1
  10.     Print #1, Split(Split(sr, "var listIssue")(1), "var stringBuilder")(0)
  11.     Close #1
  12.     MsgBox "提取完毕!"
  13. End Sub
附件:
提取网页源文件中两个指定字符串之间的内容.rar
2楼
xyf2210
学习
3楼
xmyjk
webbrowser提取法
  1. Option Explicit
  2. Sub a()
  3. Dim ie1 As Object, dmt As Object, r As Object, i As Long, x As Long, j As Long

  4. 'Load UserForm1
  5. 'UserForm1.Show 0

  6. [a1].CurrentRegion.Clear
  7. Cells.NumberFormat = "@"
  8. Set ie1 = UserForm1.WebBrowser1

  9. With ie1
  10.   .Navigate "http://video.shishicai.cn/haoma/cqssc/list/120.aspx"
  11.   Do Until .ReadyState = 4
  12.     DoEvents
  13.   Loop
  14.   Set dmt = .Document
  15. End With

  16. Set r = dmt.All.tags("table")(1).Rows
  17. For i = 0 To r.Length - 1
  18.    For j = 0 To r(i).Cells.Length - 1
  19.         Cells(i + 1, j + 1) = r(i).Cells(j).innerText
  20.    Next
  21. Next

  22. Set ie1 = Nothing
  23. Set dmt = Nothing
  24. Set r = Nothing

  25. [a1].CurrentRegion.Columns.AutoFit

  26. End Sub

开奖.rar
4楼
0Mouse
弦月,上周在E之家见识了你的这一精简解法,只是还木有来得及消化。

期待你的“网页数据抓取”班开课呢!
5楼
xmyjk
客气了,还有xmlhttp做法
  1. Option Explicit

  2. Sub test()
  3.     Dim xmlhttp As Object, arr, i&, tmp
  4.     Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
  5.     Range("a1").CurrentRegion.Offset(1).ClearContents
  6.     With xmlhttp
  7.         .Open "get", "http://video.shishicai.cn/haoma/cqssc/list/120.aspx", False
  8.         .send
  9.         tmp = Filter(Split(Split(Split(Replace(.responsetext, "|", "-"), "var listIssue = [{")(1), "}];")(0), """"), "-")
  10.         ReDim arr(UBound(tmp) \ 3, 2)
  11.         For i = 0 To UBound(tmp)
  12.             If i Mod 3 = 0 Then arr(i \ 3, i Mod 3) = Replace(tmp(i), "-", "|") Else arr(i \ 3, i Mod 3) = tmp(i)
  13.         Next
  14.     End With
  15.     [a2].Resize(UBound(arr) + 1, UBound(arr, 2) + 1) = arr
  16.     Set xmlhttp = Nothing
  17.     MsgBox "OK!"

  18. End Sub

test.rar
6楼
0Mouse
又有新鲜的养分可以补充了!
7楼
minren118
几位版主的网抓帖都是用split来提取,很少看到用正则的案例,用正则和split哪个比较好?我自己比较多用正则来提取
8楼
0Mouse
都能解决问题,不过没有比较过,你不妨比较哈。:-)
9楼
shunlibiye
高手!羡慕
10楼
老糊涂
学习

免责声明

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

评论列表
sitemap