Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ruby从URL的HTML源代码中获取文本_Html_Ruby_Ruby On Rails 3_Url_Gem - Fatal编程技术网

使用Ruby从URL的HTML源代码中获取文本

使用Ruby从URL的HTML源代码中获取文本,html,ruby,ruby-on-rails-3,url,gem,Html,Ruby,Ruby On Rails 3,Url,Gem,我已经阅读了一些关于stackoverflow的文章和帖子。如果我在stack上重复别人的帖子,我道歉。有没有一种方法可以遍历给定URL的HTML源代码并返回标题标记的文本 例如: <h2 class='title'> <a href="/blog/step-by-step-guide-to-building-your-first-ruby-gem">Step-by-Step Guide to Building Your First Ruby Gem</a>

我已经阅读了一些关于stackoverflow的文章和帖子。如果我在stack上重复别人的帖子,我道歉。有没有一种方法可以遍历给定URL的HTML源代码并返回标题标记的文本

例如:

<h2 class='title'>
<a href="/blog/step-by-step-guide-to-building-your-first-ruby-gem">Step-by-Step Guide to Building Your First Ruby Gem</a>
</h2>
有没有一个我可以做的

doc.html('h1').each do |tag| puts link.content end

我希望它是有意义的…任何对资源方向的洞察都会非常感激。

Nokogiri同时具有XPath和CSS访问器,所以您可以这样做

doc.css('h1 > a').each do |tag| puts link.content end

如果您不喜欢XPath。(或者只是
'h1'
-我不能100%确定您是否希望链接的文本出现在标题中,或者标题本身)。

很好!感谢您的快速回复,阿玛丹。我想我需要标题本身的文本。
doc.css('h1 > a').each do |tag| puts link.content end