.net
  • ruby
  • powershell
  • xpath
  • 2013-07-11 39 views 2 likes 
    2

    我把一些ruby腳本posh的XPath在PowerShell中

    > gem install nokogiri 
    
    > irb 
    
    > require 'nokogiri' 
    
    > $html = Nokogiri::HTML("<div><img src='//127.0.0.1:5598/user/first.png' /> 
             <img src='//127.0.0.1:5598/user/second.png' /></div>") 
    
    > $html.xpath('//img[contains(@src,"first")]') 
    
    # Output: <img src='//127.0.0.1:5598/user/first.png' /> 
    

    在PowerShell中,我有:

    > [System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") 
    
    > [System.Reflection.Assembly]::LoadWithPartialName("System.Xml.XPath") 
    
    > $html = [System.Xml.Linq.XDocument]::Parse("<div> 
             <img src='//127.0.0.1:5598/user/first.png' /> 
             <img src='//127.0.0.1:5598/user/second.png' /></div>") 
    
    > [System.Xml.XPath.Extensions]::XPathSelectElement($html, 
                '//img[contains(@src,"first")]') 
    
    # It displays the properties of XElement type object 
    

    如何獲得相同的輸出?

    在PowerShell v.4中解析html有更好的方法嗎?

    回答

    2

    只需加上.ToString()即可獲得相同的輸出。

    下面是一個簡單的選擇產生相同:

    $html = [xml] "<div><img src='//127.0.0.1:5598/user/first.png' /> 
            <img src='//127.0.0.1:5598/user/second.png' /></div>" 
    $html.SelectSingleNode('//img[contains(@src,"first")]').OuterXml 
    

    甚至

    ($html.div.img | ?{ $_.src -match 'first' }).outerxml 
    

    請注意,我假設你正在處理XML按你自己的PowerShell的例子(我不是用於處理HTML)...

    1

    使用invoke-webrequest(PS V3)的另一種替代方法:

    $ie = new-object -com "InternetExplorer.Application" 
    $ie.Navigate("c:\temp\test.html") 
    $html=$ie.Document 
    $html.images|% { if ($_.src -match "first") {echo $_.outerHTML}} 
    

    請注意,如果它不是一個本地文件,你可以使用:

    $html = Invoke-WebRequest "http://yourURL" 
    

    然後只需用小命令解析$html.ParsedHtml.body

    +0

    善於思考,好方法!不要忘記關閉IE,但:] – mousio

    2

    另一種方式查詢XML:

    $xml = [xml]@" 
    <div> 
    <img src='//127.0.0.1:5598/user/first.png' /> 
    <img src='//127.0.0.1:5598/user/second.png' /> 
    </div> 
    "@ 
    
    (select-xml -xml $xml -xpath '//img[contains(@src,"first")]') | % { $_.node.src } 
    
    相關問題