0

我最近从 excel VBA 自动化转移到尝试基于http://the-automator.com/web-scraping-intro-with-autohotkey/教程的 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("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "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("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}

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

4

1 回答 1

2

要在活动窗口的活动选项卡上工作(如果它是 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

在您的链接中,我认为“名称”指的是 LocationName (标签的标题):

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

对于整个页面的innerText:

MsgBox % wb.document.documentElement.innerText

高温高压

于 2017-02-16T00:16:29.250 回答