我把一些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有更好的方法嗎?
善於思考,好方法!不要忘記關閉IE,但:] – mousio