Kre*_*eki 29 ruby ruby-on-rails nokogiri
我有一个看起来像这样的文件:
<div id="block">
<a href="http://google.com">link</a>
</div>
Run Code Online (Sandbox Code Playgroud)
我无法让Nokogiri获得href
属性的价值.我想将地址存储在Ruby变量中作为字符串.
Mic*_*ohl 46
html = <<HTML
<div id="block">
<a href="http://google.com">link</a>
</div>
HTML
doc = Nokogiri::HTML(html)
doc.xpath('//div/a/@href')
#=> [#<Nokogiri::XML::Attr:0x80887798 name="href" value="http://google.com">]
Run Code Online (Sandbox Code Playgroud)
或者,如果你想更具体的div:
>> doc.xpath('//div[@id="block"]/a/@href')
=> [#<Nokogiri::XML::Attr:0x80887798 name="href" value="http://google.com">]
>> doc.xpath('//div[@id="block"]/a/@href').first.value
=> "http://google.com"
Run Code Online (Sandbox Code Playgroud)
小智 27
doc = Nokogiri::HTML(open("[insert URL here]"))
href = doc.css('#block a')[0]["href"]
Run Code Online (Sandbox Code Playgroud)
该变量href
被赋值给具有id "href"
的<a>
元素内元素的属性值'block'
.该行doc.css('#block a')
返回包含属性的单个项目数组#block a
. [0]
定位单个元素,它是包含所有属性名称和值的哈希. ["href"]
定位该"href"
哈希内部的键并返回值,该值是包含url的字符串.
fea*_*ool 17
在以各种形式挣扎于这个问题后,我决定自己写一个伪装成答案的教程.它可能对其他人有帮助.
从这个片段开始:
require 'rubygems'
require 'nokogiri'
html = <<HTML
<div id="block1">
<a href="http://google.com">link1</a>
</div>
<div id="block2">
<a href="http://stackoverflow.com">link2</a>
<a id="tips">just a bookmark</a>
</div>
HTML
doc = Nokogiri::HTML(html)
Run Code Online (Sandbox Code Playgroud)
我们可以使用xpath或css来查找所有元素,然后只保留具有href
属性的元素:
nodeset = doc.xpath('//a') # Get all anchors via xpath
nodeset.map {|element| element["href"]}.compact # => ["http://google.com", "http://stackoverflow.com"]
nodeset = doc.css('a') # Get all anchors via css
nodeset.map {|element| element["href"]}.compact # => ["http://google.com", "http://stackoverflow.com"]
Run Code Online (Sandbox Code Playgroud)
但是有一种更好的方法:在上面的例子中,这.compact
是必要的,因为搜索也返回"只是一个书签"元素.我们可以使用更精细的搜索来查找包含href
属性的元素:
attrs = doc.xpath('//a/@href') # Get anchors w href attribute via xpath
attrs.map {|attr| attr.value} # => ["http://google.com", "http://stackoverflow.com"]
nodeset = doc.css('a[href]') # Get anchors w href attribute via css
nodeset.map {|element| element["href"]} # => ["http://google.com", "http://stackoverflow.com"]
Run Code Online (Sandbox Code Playgroud)
要查找内的链接 <div id="block2">
nodeset = doc.xpath('//div[@id="block2"]/a/@href')
nodeset.first.value # => "http://stackoverflow.com"
nodeset = doc.css('div#block2 a[href]')
nodeset.first['href'] # => "http://stackoverflow.com"
Run Code Online (Sandbox Code Playgroud)
如果您知道自己只搜索一个链接,则可以使用at_xpath
或at_css
代替:
attr = doc.at_xpath('//div[@id="block2"]/a/@href')
attr.value # => "http://stackoverflow.com"
element = doc.at_css('div#block2 a[href]')
element['href'] # => "http://stackoverflow.com"
Run Code Online (Sandbox Code Playgroud)
如果您知道与链接相关联的文本并想要查找其网址,该怎么办?一个小的xpath-fu(或css-fu)派上用场:
element = doc.at_xpath('//a[text()="link2"]')
element["href"] # => "http://stackoverflow.com"
element = doc.at_css('a:contains("link2")')
element["href"] # => "http://stackoverflow.com"
Run Code Online (Sandbox Code Playgroud)
如果您想查找与特定链接相关的文本,该怎么办?没问题:
element = doc.at_xpath('//a[@href="http://stackoverflow.com"]')
element.text # => "link2"
element = doc.at_css('a[href="http://stackoverflow.com"]')
element.text # => "link2"
Run Code Online (Sandbox Code Playgroud)
除了广泛的Nokorigi文档之外,我在编写本文时遇到了一些有用的链接:
doc = Nokogiri::HTML("HTML ...")
href = doc.css("div[id='block'] > a")
result = href['href'] #http://google.com
Run Code Online (Sandbox Code Playgroud)