SciTe 自动热键获取活动浏览器页面的内部文本



我最近从 excel VBA 自动化转向尝试基于 http://the-automator.com/web-scraping-intro-with-autohotkey/教程的自动热键自动化,但我似乎不能很好地理解代码,有人可以指出我正确的方向吗?

我正在尝试使我的 F1 键抓取当前活动的一些数据。

F1::
pwb := ComObjCreate("InternetExplorer.Application") ;create IE Object
pwb.visible:=true  ; Set the IE object to visible
pwb := WBGet()
;************Pointer to Open IE Window******************
WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleaccObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}

我知道这段代码创建了一个新的 IE 应用程序,但是如果我不想创建一个怎么办?这只是为了获取当前活动窗口?我看到一些代码允许我获取当前活动的浏览器 URL,但我似乎无法获取当前活动的浏览器元素。

到目前为止,我已经尝试过了。有人可以告诉我如何让它指向活动页面并获取其一些数据吗?

F1::
wb := WBGet()
if !instr(wb.LocationURL, "https://www.google.com/")
{
   wb := ""
   return
}
doc := wb.document
h2name    := rows[0].getElementsByTagName("h2")

FileAppend, %h2name%, Somefile.txt
Run Somefile.txt
return


WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleaccObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}

尝试测试变量是否会写入某个文件.txt,不太确定它应该如何使用 msgbox 进行测试。它一直在编写整个脚本,而不是显示结果。

要在活动窗口的活动选项卡(如果是 Internet Explorer 窗口(上工作,请执行以下操作:

q::
WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
if !(vWinClass = "IEFrame")
Return
wb := WBGet("ahk_id " hWnd)
MsgBox % wb.document.activeElement.tagName "`r`n" wb.document.activeElement.innerText
wb := ""
Return

要在第一个找到的 Internet Explorer 窗口的活动选项卡上工作,请执行以下操作:

w::
WinGet, hWnd, ID, ahk_class IEFrame
wb := WBGet()
;wb := WBGet("ahk_class IEFrame") ;this line is equivalent to the one above
MsgBox % wb.document.activeElement.tagName "`r`n" wb.document.activeElement.innerText
wb := ""
Return

关于h2name,我不相信这会做任何事情,因为"行"没有在脚本中的任何位置定义。

h2name    := rows[0].getElementsByTagName("h2")

以下方法可能有效:

h2name := ""
try h2name := wb.document.getElementsByTagName("h2").item[0].name
MsgBox % h2name
MsgBox % wb.document.getElementsByTagName("h2").item[0].tagName
MsgBox % wb.document.getElementsByTagName("h2").item[0].innerText

在您的链接中,我认为"名称"是指位置名称(选项卡的标题(:

MsgBox % wb.LocationName
MsgBox % wb.document.title ;more reliable

对于整个页面的内部文本:

MsgBox % wb.document.documentElement.innerText

最新更新