html - VBA如何发送“ENTER”来执行Web
问题描述
VBA如何发送“ENTER”来执行Web?在Web中输入“5904”时,需要在键盘中手动按“ENTER”。那么VBA如何在自动按下它的网页。
Public Sub Web()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.tpex.org.tw/web/stock/statistics/monthly/st44.php?l=zh-tw"
While .Busy Or .readyState < 4: DoEvents: Wend
Set ticker = ie.document.getElementById("input_stock_code")
ticker.Value = "5904"
End With
End Sub
解决方案
该页面执行 xhr POST 请求,因此您可以模仿添加预期的Content-Type
标头。我使用剪贴板写出table
与类名匹配的内容,但您可以循环节点的trs
和。tds
table
Option Explicit
Public Sub WriteOutMonthlyStockTrading()
'VBE>Tools>References>Microsoft HTML Object Library
Dim http As Object, html As MSHTML.HTMLDocument
Set http = CreateObject("MSXML2.XMLHTTP")
Set html = New MSHTML.HTMLDocument
With http
.Open "POST", "https://www.tpex.org.tw/web/stock/statistics/monthly/st44.php?l=zh-tw", False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send "yy=2019&input_stock_code=5904"
html.body.innerHTML = .responseText
End With
Dim clipboard As Object
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
clipboard.SetText html.querySelector(".page-table-board").outerHTML
clipboard.PutInClipboard
ThisWorkbook.Worksheets("Sheet1").Range("A1").PasteSpecial
End Sub
推荐阅读
- laravel - Laravel 写入文件流
- java - SQL没有将文件内容添加到表中
- android-studio - Android Studio Gradle:如何在自定义资产处理器任务中获得变体/风味
- hive - Where 子句 (HIVE) 中连接与子查询的性能
- reactjs - 无法在反应 webpack 应用程序中解析 './util' 倍增
- android - 缩放 ImageView 以适应而不拉伸
- ruby - 如何将 .sc 文件重写为不同的格式?
- c# - 使用存储过程 For XML Path 直接读入绑定到 DataGridView 的 DataSet?
- latex - 将gnuplot与latex结合:方法和输出
- excel - 强制将短日期转换为英国格式 DD-MM-YYYY