我最近从Excel VBA自动化移动基于http://the-automator.com/web-scraping-intro-with-autohotkey/教程试用AutoHotkey的自动化,但我似乎无法很好地理解代码,可能有人请点我在正确的方向?赛特AutoHotkey的获得活动的浏览器页面的的innerText
我想让我的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进行测试。它一直在写整个脚本而不是显示结果。
您可以使用UrlDownloadToFile,http://www.example.com,sourcecode.html'命令将(全部)代码保存到PC中,然后您可以解析标签外部的文本<> '(不包括''和''之间的文字)来获取页面的内文。 –