【问题标题】:AutoHotKey How to get the text from a web page in Internet ExploreAutoHotKey 如何从 Internet Explorer 中的网页中获取文本
【发布时间】:2016-04-03 02:58:46
【问题描述】:

我必须在工作中向第 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,并阅读了文档here。但这不起作用,因为我需要的供应商网站信息是通过表单请求的,它没有我可以使用的静态网站链接。

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

【问题讨论】:

    标签: internet-explorer automation autohotkey


    【解决方案1】:

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

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

    检索文本。如果你不知道变量名,但知道索引号 i,或者可以通过索引号循环查找特定的一些文本,有类似的东西:

    wb.document.all[i].innerText

    第,

    【讨论】:

      【解决方案2】:

      您可以更进一步,直接与 API 进行通信。这种方式更快、更可靠,并且可以完全隐藏在后台运行,而不会影响用户体验。

      你只需要找出三件事:

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

      您可以通过查看 HTML 代码或使用浏览器的开发人员工具或使用 postman 等插件或使用 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
      

      您可能还想看看这个:How to do logins using the WinHttpRequest COM?

      【讨论】:

        猜你喜欢
        • 2017-10-19
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        • 2011-12-13
        • 2019-06-27
        • 2010-09-09
        • 2011-11-03
        • 1970-01-01
        相关资源
        最近更新 更多