AutoHotKey 如何从 Internet Explore 中的网页获取文本

AutoHotKey How to get the text from a web page in Internet Explore

我必须在工作中向第 3 方供应商网站发送一些状态请求。每天500-600次。我正在尝试自动执行此任务。目前我的代码使用以下方法。

; Helper function to get the text from current web page
CurrentScreen() {
    Sleep, 500
    MouseClick, left,  880, 240
    Send, {CTRLDOWN}a{CTRLUP}
    Sleep, 500
    Send, {CTRLDOWN}c{CTRLUP}
    Sleep, 500
    return Clipboard
}

; Helper function to read at line number X and return text
GetInformation(webpageBuffer) {
   If (A_Index == 30)
      ; Do something here and return data
}

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("www.someVendor.com")
IELoad(wb)

; Do a lot of clicking and searching
someText := GetInformation(CurrentScreen())

我在网上搜索发现 URLDownloadToFile and I read the documentation here。但这不起作用,因为我需要的供应商网站信息是通过表单请求的,它没有静态网站link我可以使用。

我目前的方法可行,但速度相当慢,因为我必须额外花 2-3 秒阅读每一页,而且我的程序在 运行(来自复制和粘贴)。是否有其他解决方案可以从 Internet Explorer 页面加载中读取文本,而不使用复制、粘贴、读取剪贴板方法?

如果您知道包含所需文本的确切元素名称,请使用

var := wb.Document.getElementByID(Element_Name).innerText

检索文本。如果您不知道变量名,但知道索引号 i,或者可以遍历索引号特别是查找一些文本,则有类似的东西:

wb.document.all[i].innerText

Hth,

您可以走得更远,直接与 API 交流。这种方式更快、更可靠,并且可以 运行 完全隐藏在后台而不影响用户体验。

你只需要找出三件事:

  • 提交的表单是什么?
  • 表格使用什么方法? (POST、获取、...)
  • 表单向哪个URL提交数据?

您可以通过查看 HTML 代码或使用浏览器的开发人员工具或使用像邮递员这样的附加程序或使用像 Fiddler 这样的独立程序实际记录表单提交来找到所有这些。

这是最终可能的样子的示例:

formTargetUrl := "www.someVendor.com"
formMethod := "POST"
fromData := "exampleKey=exampleValue&email=me@something.com&foo=bar"

HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpObj.Open(formMethod,formTargetUrl)
HttpObj.Send(fromData)

rawHtmlResponse := HttpObj.ResponseText

;now you could use regex to find the text, 
;you could also dump rawHtmlResponse to a text file, 
;or you could use the HTMLfile object if you can identify the html element by its id, name, class, tagname, etc.

document := ComObjCreate("HTMLfile")
document.write(rawHtmlResponse)

textElement := document.getElementById("someId")
;you may also try:
;textElement := document.getElementsByName("someName")[1] ;returns multiple results (not sure if 1 or 0 is the first result)
;textElement := document.getElementsByTagName("someTagName")[1] ;returns multiple results (not sure if 1 or 0 is the first result)
;textElement := document.getElementsByClassName("someClassName")[1] ;returns multiple results (not sure if 1 or 0 is the first result)

MsgBox % textElement.innerText
;you may also try
;MsgBox % textElement.textContent
;MsgBox % textElement.innerHTML
;MsgBox % textElement.value

你可能还想看看这个: