我的第一個問題,在這裏,將是真棒找到答案。我是使用nokogiri的新手。提取內容屬性的內容?
這裏是我的問題。我有這樣的事情在HTML頭對目標網站(這裏是TechCrunch的帖子):
<meta content="During my time at TechCrunch I've seen thousands of startups and written about hundreds of them. I sure as hell don't know all ..." name="description"/>
我現在想有一個腳本通過元標記運行,找到一個名爲屬性「描述「並獲取內容屬性中的內容。
我已經試過這樣的事情
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.techcrunch.com/2009/10/11/the-underutilized-power-of-the-video-demo-to-explain-what-the-hell-you-actually-do/"
doc = Nokogiri::HTML(open(url))
posts = doc.xpath("//meta")
posts.each do |link|
a = link.attributes['name']
b = link.attributes['content']
end
後,我可以選擇其中屬性名稱等於說明中的鏈接 - 但是這個代碼返回nil a和b。
我玩過 posts = doc.xpath("//meta")
,posts = doc.xpath("//meta/*")
等,但仍然無。
問題不在於xpath,因爲它似乎沒有解析文檔。你可以用'puts doc'來檢查它,它不包含完整的輸入。 – akuhn 2010-01-05 01:43:35