尝试使用ahk脚本捕获类标记中的值

时间:2014-07-29 22:10:43

标签: autohotkey

我正试图在http://nominatim.openstreetmap.org/search.php?q=MK3+5JE&viewbox=-147.13%2C72.78%2C147.13%2C-55.67处抓取<span class="latlon"></span>中的值:

例如,在这种情况下51.99,-0.76

enter image description here

但每当我运行我的AHK脚本时,这就是输出:

enter image description here

为什么它没有读取latlon字段中的值?

到目前为止,这是我的代码:

Loop, read, test.csv
{
Loop, parse, A_LoopReadLine, %A_Tab%
{
    ; Run IE
    IE :=  ComObjCreate("InternetExplorer.Application")
    IE.Visible:=True

    ; Copy current postcode row to clipboard
    Clipboard = %A_LoopField%
    Postcode = %A_LoopField%
    ClipWait                            

    ; Debugging - wait 1s then check output
    ; Sleep 1000
    ; MsgBox, %Clipboard%

    ; Navigate to Bing Maps and paste the postcode
    IE.Navigate("http://nominatim.openstreetmap.org/")
    Sleep 300
    Send, ^v
    Send {Enter}

    ; Debugging - wait 1s then check output
    ; Sleep 1000
    ; IE.Navigate("javascript: alert(document.getElementsByClassName('name')[0].innerHTML)")
    ; IE.Navigate("javascript: alert(document.getElementsByClassName('latlon')[0].innerHTML)")

    ; Collect results
    j := 0
    i := 1
    Addr := {}
    while (i <= 1)
    {
        Sleep 1000
        Addr[i] := IE.document.getElementsByClassName("name")[j].innertext
        LatLon[i] := IE.document.getElementsByClassName("latlon")[j].innertext
        Addr_Object := StrSplit(Addr[i], "`,")
        LatLon_Object := StrSplit(LatLon[i], "`,")
        If (Substr(Addr[i], 1, 2) = "MK")
        {
            Addr[i] := Addr_Object[2] . "," . Trim(Addr_Object[3]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
            MsgBox, % Addr[i]
        }
        Else
        {
            Addr[i] := Addr_Object[1] . "," . Trim(Addr_Object[2]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
            MsgBox, % Addr[i]
        }

    j++  
    i++
    }

    ; Close IE
    IE.quit()
}   
}

test.csv的内容:

MK3 5JE
MK1 1AS

会欣赏正确方向的任何指示。

1 个答案:

答案 0 :(得分:2)

我认为你必须更准确一点......嗯,成为......不会有什么坏处。

document.getElementById("searchresults").getElementsByClassName("result")[0].getElementsByClassName("latlon")[0].innerHTML

以下是一个例子: 您可以更改为循环所有结果,但在此示例中,仅检索第一个结果。

SetWorkingDir, %A_scriptdir%
FileRead,data,test.csv
data := StrSplit(data,"`r`n",A_Tab)

Gui Add, ActiveX, xm w640 h480 vWB, Shell.Explorer
ComObjConnect(WB, WB_events)  ; Connect WB's events to the WB_events class object.
Gui Show
WB.silent := true ;Surpress JS Error boxes
ProcessDone:=0 ; "Universal" signal
for each, item in data
{
    ToolTip Loading...`nPlease wait...
    PostCode:=item
    WB.Navigate("http://nominatim.openstreetmap.org/search.php?q=" item) ;search it
    while (!ProcessDone) {
        ;wait
    }
    ProcessDone:=0
}
return

class WB_events
{
    DocumentComplete(wb, NewURL)
    {
        global ProcessDone
        global PostCode
        while (StrLen(wb.document.getElementById("searchresults").innerHTML)==0) {
            ;wait
        }
        /* Information nested as:
            #searchresults
                .result
                    .latlon
        */
        numResult := wb.document.getElementById("searchresults").getElementsByClassName("result").length
        addr := wb.document.getElementById("searchresults").getElementsByClassName("result")[0].getElementsByClassName("name")[0].innerHTML
        coords := wb.document.getElementById("searchresults").getElementsByClassName("result")[0].getElementsByClassName("latlon")[0].innerHTML
        ToolTip
        MsgBox,,OpenStreenMap - First result,Address:`t%addr%`nPostal code:`t%PostCode%`nCoordinates:`t%coords%`nNumber of results:`t%numResult%`nURL: %NewURL%
        ProcessDone:=1
    }
}

GuiClose:
ExitApp