使用Ruby从URL的HTML源代码中获取文本
我已经阅读了一些关于stackoverflow的文章和帖子。如果我在stack上重复别人的帖子,我道歉。有没有一种方法可以遍历给定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>
<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