ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 361|回复: 3

[求助] 网抓-MSXML2.XMLHTTP库

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-4-19 20:14 | 显示全部楼层 |阅读模式
本人在学习网抓。我先利用Python库urlopen成功读取了一个网页的内容,Python代码为
  1. from urllib.request import urlopen
  2. html = urlopen('http://pythonscraping.com/pages/page1.html')
  3. print(html.read())
复制代码
打印结果为
b'<html>\n<head>\n<title>A Useful Page</title>\n</head>\n<body>\n<h1>An Intereiqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco lsting Title</h1>\n<div>\nLorem ipsum dolor sit amet, consectetur adipisicing ehenderit in voluptate velit esse cillum dolore eu fugiat nulla parlit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut eniia deserunt mollit anim id est laborum.\n</div>\n</body>\n</html>\m ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n</div>\n</body>\n</html>\n'
我现在想用VBA实现同样的效果,并在VBE中写入如下代码:
  1. Public Sub parsehtml()
  2.     Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement
  3.     Dim i As Integer
  4.     Set http = CreateObject("MSXML2.XMLHTTP")
  5.     http.Open "GET", "https://pythonscraping.com/pages/page1.html", False
  6.     http.send
  7.     html.body.innerHTML = http.responseText
  8.     Debug.Print html
  9. End Sub
复制代码
但是立即窗口跳出来的是“[object HTMLDocument]”。所以想请问一下需要调取HTMLDocument的什么属性才可以实现上述效果?



TA的精华主题

TA的得分主题

发表于 2023-4-20 10:53 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
http.responseText 不就是网页源代码吗?   Debug.Print http.responseText

TA的精华主题

TA的得分主题

发表于 2023-4-20 11:10 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-4-20 14:32 | 显示全部楼层
perfect131 发表于 2023-4-20 11:10
就是获取动态源码

感谢回复。经过测试,您提出的两个解决方案都可以运行。
1. Debug.Print html.all(0).outerHTML
2. Debug.Print http.responseText
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-9-27 22:17 , Processed in 0.030429 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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