繁体   English   中英

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

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

我必须在工作中向第三方供应商网站提出一些状态请求。 这是每天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,并在此处阅读文档。 但这是行不通的,因为通过表格请求了我需要的供应商网站信息,但它没有我可以使用的静态网站链接。

我当前的方法有效,但是速度很慢,因为我在要阅读的每一页上都花了2-3秒的时间,并且程序运行时(从复制和粘贴)我的程序将以蓝色和白色闪烁。 是否有其他解决方案可以从Internet Explorer页面加载中读取文本,而不使用复制,粘贴,读取剪贴板方法?

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

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

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

wb.document.all[i].innerText

心连心,

您甚至可以走得更远,直接与API通信。 这样可以更快,更可靠,并且可以完全隐藏在后台运行,而不会影响用户体验。

您只需要找出三件事:

  • 提交什么表格?
  • 表格使用什么方法? (POST,GET等)
  • 表单将数据提交到哪个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

您可能还想看一下: 如何使用WinHttpRequest COM登录?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM