如何从AHK的HTML代码中检索一些信息? [英] How do I retrieve some information from HTML code in AHK?

查看:70
本文介绍了如何从AHK的HTML代码中检索一些信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从HTML代码中检索一些信息.让我们考虑以下内容:

I'd like to retrieve some information from HTML code. Let's consider the following:

<ul class="article-additional-info">
   <li><strong>Issue Year:</strong> 2011</li>
   <li><strong>Issue No:</strong> 1 (200)</li>
   <li><strong>Page Range:</strong> 65-80</li>
   <li><strong>Page Count:</strong> 15</li>
   <li><strong>Language:</strong> Polish</li>
</ul>

我可以使用document.getElementsByClassName("article-additional-info")[0].innerTextarticle-additional-info类中获取所有信息.

I can get all the information from article-additional-info class by using document.getElementsByClassName("article-additional-info")[0].innerText.

但是我如何从此类中检索单个信息,例如2011(来自<strong>Issue Year:</strong> 2011<)呢? 我想避免使用RegEx.

But how do I retrieve individual information from this class like 2011 (from <strong>Issue Year:</strong> 2011<)? I'd like to avoid using RegEx.

基于答案,我对代码进行了一些修改.但是,我无法摆脱一个要素:Language:.这是代码:

Based on the answer, I slightly modified the code. However, I cannot get rid of one element: Language:. Here's the code:

html =
(
<body>
<ul class="article-additional-info">
   <li><strong>Issue Year:</strong> 2011</li>
   <li><strong>Issue No:</strong> 1 (200)</li>
   <li><strong>Page Range:</strong> 65-80</li>
   <li><strong>Page Count:</strong> 15</li>
   <li><strong>Language:</strong> Polish</li>
</ul>
</body>
)

document := ComObjCreate("HTMLfile")
document.write(html)

test := ["Issue Year:", "Issue No:", "Page Range:", "Page Count:"]

try While (x := document.getElementsByTagName("ul")[A_Index-1])
    {
    if (x.className = "article-additional-info")
        {
        count++
        yclass%count% := x.innerHTML
        }
    }

loop, %count%
{
html := yclass%A_Index%
document.Close
document := ComObjCreate("HTMLfile")
document.write(html)

try While (x := document.getElementsByTagName("strong")[A_Index-1])
    {
    StringLen, y, % test[A_Index]
    msgbox % [A_Index] . " " . substr(x.parentnode.innerText, y+2)
    }
}
ExitApp

推荐答案

尝试一下:

html =
(
<body>
<ul class="article-additional-info">
   <li><strong>Issue Year:</strong> 2011</li>
   <li><strong>Issue No:</strong> 1 (200)</li>
   <li><strong>Page Range:</strong> 65-80</li>
   <li><strong>Page Count:</strong> 15</li>
   <li><strong>Language:</strong> Polish</li>
</ul>
<ul class="article-additional-info">
   <li><strong>Issue Year:</strong> XX 2011</li>
   <li><strong>Issue No:</strong> XX 1 (200)</li>
   <li><strong>Page Range:</strong> XX 65-80</li>
   <li><strong>Page Count:</strong> XX 15</li>
   <li><strong>Language:</strong> XX Polish</li>
</ul>
</body>
)

test := "Language:"  ;  adjust for the variable you want to return
classno := 1  ;  adjust the number for the correct class instance!

document := ComObjCreate("HTMLfile")
document.write(html)

try While (x := document.getElementsByTagName("ul")[A_Index-1])
    {
    if (x.className = "article-additional-info")
        yclass%A_Index% := x.innerHTML
    }
html := yclass%classno%

document.Close
document := ComObjCreate("HTMLfile")
document.write(html)

try While (x := document.getElementsByTagName("strong")[A_Index-1])
    {
    StringLen, y, test
    if (x.innerText = test)
        msgbox % substr(x.parentnode.innerText, y+2)  ;  returns "Polish"
    }
ExitApp

而且,如果要遍历所有类实例和所有变量,则可以这样做:

And, if you want to iterate over all the class instances and for all the variables, just do it like so:

html =
(
<body>
<ul class="article-additional-info">
   <li><strong>Issue Year:</strong> 2011</li>
   <li><strong>Issue No:</strong> 1 (200)</li>
   <li><strong>Page Range:</strong> 65-80</li>
   <li><strong>Page Count:</strong> 15</li>
   <li><strong>Language:</strong> Polish</li>
</ul>
<ul class="ao">
   <li><strong>Issue Year:</strong> zz 2011</li>
   <li><strong>Issue No:</strong> zz 1 (200)</li>
   <li><strong>Page Range:</strong> zz 65-80</li>
   <li><strong>Page Count:</strong> zz 15</li>
   <li><strong>Language:</strong> zz Polish</li>
</ul>
<ul class="article-additional-info">
   <li><strong>Issue Year:</strong> XX 2011</li>
   <li><strong>Issue No:</strong> XX 1 (200)</li>
   <li><strong>Page Range:</strong> XX 65-80</li>
   <li><strong>Page Count:</strong> XX 15</li>
   <li><strong>Language:</strong> XX Polish</li>
</ul>
</body>
)

document := ComObjCreate("HTMLfile")
document.write(html)

; To skip a variable, change it to: "" (as shown, where only first 3 are shown
test := ["Issue Year:", "Issue No:", "Page Range:", "", ""]

try While (x := document.getElementsByTagName("ul")[A_Index-1])
    {
    if (x.className = "article-additional-info")
        {
        count++
        yclass%count% := x.innerHTML
        }
    }

loop, %count%
{
which++
html := yclass%A_Index%

document.Close
document := ComObjCreate("HTMLfile")
document.write(html)

try While (x := document.getElementsByTagName("strong")[A_Index-1])
    {
    StringLen, y, % test[A_Index]
    if (test[A_Index] <> "")
        msgbox % which . ": " . test[A_Index] . " " . substr(x.parentnode.innerText, y+2)
    }
}
ExitApp

substr(x.parentnode.innerText, y+2)是您要查找的值.

玩得开心!

这篇关于如何从AHK的HTML代码中检索一些信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆