2017-02-11 58 views
0

我最近从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进行测试。它一直在写整个脚本而不是显示结果。

+1

您可以使用UrlDownloadToFile,http://www.example.com,sourcecode.html'命令将(全部)代码保存到PC中,然后您可以解析标签外部的文本<> '(不包括''和''之间的文字)来获取页面的内文。 –

回答

1

要在活动窗口的活动标签上工作(如果它是一个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 

HTH

相关问题