【问题标题】:How can I strip the element using vbscript and display in message box?如何使用 vbscript 剥离元素并显示在消息框中?
【发布时间】:2012-05-24 16:11:16
【问题描述】:

我想查找 2 年合同的价格并将其显示在消息框中。到目前为止我有:

Dim MyPage
Dim Price
Set MyPage=CreateObject("Microsoft.XMLDOM")
MyPage.load("http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723")
Wscript.Sleep 2000
Set Price = MyPage.getElementsByTagName("span")
For Each Elem In Price
   MsgBox(Elem.firstChild.nodeValue) 
Next

我明白我完全错了,但我什至不知道从哪里开始。我喜欢编写这样的简单程序,但我只需要帮助开始。任何想法都会有所帮助!

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    这里是一个更好的版本,使用 HTMLFile 对象

    Dim HTMLDoc, XML, URL, table
    Set HTMLDoc = CreateObject("HTMLFile")
    Set XML = CreateObject("MSXML2.XMLHTTP")
    URL = "http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723"
    
    With XML
      .Open "GET", URL, False
      .Send
      HTMLDoc.Write .responseText
    End With
    
    Set spans = HTMLDoc.getElementsByTagName("span")
    for each span in spans
      WScript.Echo span.innerHTML
    next
    
    '=><SPAN>Set Location</SPAN>
    '=>Set Location
    '=><SPAN>Submit</SPAN>
    '=>Submit
    '=>Connect with us
    

    【讨论】:

    • 是否可以注入 cookie,以便我可以看到插入位置的页面?
    • 不知道,它是 IE,所以如果你之前在 IE 中访问网站获得了 cookie,并且你的 IE 设置没问题,我猜 cookie 对这个会话也有好处
    • 我的意思是它是用来获取页面的IE浏览器
    • 无法做出使用哪个浏览器的响应,但由于控件是微软的,我在工作中测试了它,我只有 IE,我认为只有 IE,也许使用了默认浏览器,在这种情况下,我的建议是通过使用默认浏览器手动访问网站来获取 cookie
    • 否则你必须创建一些 javascript 来获取 cookie 并在 HTMLFile 对象中运行它,抱歉没有这样的例子
    【解决方案2】:

    您使用的控件是用于读取 XML 文档的,您需要这样的东西

    'Create an xmlhttp object, the string depends on the version that is installed 
    'on your pc could eg also be "Msxml2.ServerXMLHTTP.5.0"
    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    xmlhttp.Open "GET", "http://admin:pasword@10.0.0.2/doc/ppp.htm", False 
    xmlhttp.Send
    text=xmlhttp.responseText
    wscript.echo text
    Set xmlhttp = Nothing
    

    在您的注册表中搜索 XMLHTTP 以获取标识符的正确字符串/版本。

    要从 html 中获取标签,您可以使用以下代码

    text = "blabla <span>this is what i need</span> bla bla<span>second item</span> end"
    
    function getElementsByTagName(sTextToSeachIn, tag)
      answer = ""
      separator = ""
      set oRegExpre = new RegExp
      with oRegExpre
        .IgnoreCase = true
        .Global = true
        .MultiLine = True
        .Pattern = "<" & tag & ">(.*?)</" & tag & ">"
      end with
      set oColMatches = oRegExpre.Execute(sTextToSeachIn)
      for each match in oColMatches
        answer = answer & separator & match.subMatches(0)
        separator = "|" 'use something that's not in the spancontents
      next
      if separator <> "" then
        getElementsByTagName = split(answer, separator)
      else
        getElementsByTagName = array()
      end if
    end function
    
    for each tag in getElementsByTagName(text, "span")
      wscript.echo tag
    next
    
    '=>this is what i need
    '=>second item
    

    有比 vbscript 更好的技术,当然也有更好的语言来做到这一点,我建议看看 Ruby 在这些事情上的表现。

    【讨论】:

    • 谢谢你,帮了很多忙……但我仍然需要一些帮助来处理某个标签……
    【解决方案3】:

    Alex,作为对您关于获取 cookie 并在 HTMLFile 中运行 javascript 的评论的回应,我在这里找到了一个 ruby​​ 脚本,希望它在某个时候对您有所帮助,它读取一个页面,将其传递给 HTLMFile 对象并在该 DOM 执行一个远程 javascript 文件。它还让您了解 activeX 和 Ruby 的综合强大功能。

    require "win32ole"
    
    $jsxpath_uri = "http://svn.coderepos.org/share/lang/javascript/javascript-xpath/trunk/release/javascript-xpath-latest-cmp.js"
    uri, xpath = "http://gist.github.com/gists", "//div[@class='info']/span/a"
    
    http = WIN32OLE.new('MSXML2.XMLHTTP')
    http.Open "GET", uri, false
    http.Send
    text = http.responseText
    
    dom = WIN32OLE.new("htmlfile")
    dom.Write(text)
    dom.parentWindow.eval(open($jsxpath_uri){|f| f.read })
    
    items = dom.evaluate(xpath, dom, nil, 7, nil)
    len = items.snapshotLength
    (0...len).each do |i|
      item = items.snapshotItem(i)
      puts item.innerHTML
    end
    

    【讨论】:

    • 我在寻找 VB,而不是 ruby​​。
    • 看看“dom.parentWindow.eval”部分,这是win32ole,所以在VB中也可以使用HTMLFile Dom
    • 他们用它来教孩子们编程,你可以真正快速地开始编写富有成效的脚本,另一方面它和 python 一样强大,使用这些高级功能当然需要更多时间,看看一些youtube movs,如果它在操作系统或Linux中,不要让它们吓到你,在Windows中它是一样的。大量网站,例如ruby.bastardsbook.com/toc
    猜你喜欢
    • 2012-02-12
    • 2018-05-08
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多