1

これを行うための最速の方法は何でしょうか。

「Instructions」という単語に続いて数行の指示が含まれている可能性がある (または含まれていない可能性がある) html ドキュメントが多数あります。「Instructions」という単語とそれに続く行を含むこれらのページを解析したいと考えています。

4

3 に答える 3

1

たぶん、この線に沿った何か

require 'rubygems'
require 'nokogiri'

def find_instructions doc
  doc.xpath('//body//text()').each do |text|
    instructions = text.content.select do |line|
      # flip-flop matches all sections starting with
      # "Instructions" and ending with an empty line
      true if (line =~ /Instructions/)..(line =~ /^$/) 
    end
    return instructions unless instructions.empty?
  end
  return []
end

puts find_instructions(Nokogiri::HTML(DATA.read))


__END__
<html>
<head>
  <title>Instructions</title>
</head>
<body>
lorem
ipsum
<p>
lorem
ipsum
<p>
lorem
ipsum
<p>
Instructions
- Browse stackoverflow
- Answer questions
- ???
- Profit

More
<p>
lorem
ipsum
</body>
</html>
于 2009-12-11T10:50:47.273 に答える
0

ドキュメントが一致するかどうかをテストすることから始めることができます。

if open('docname.html').read =~ /Instructions/
  # Parse to remove the instructions.
end

Hpricot を使用して、必要な部分を抽出することをお勧めします。これは、html の構造によって多少難しくなります。より具体的なヘルプが必要な場合は、構造に関する詳細を投稿してください。

于 2009-12-11T04:54:45.550 に答える
0

これは最も「正しい」方法ではありませんが、ほとんどの場合は機能します。正規表現を使用して文字列を検索します: ruby​​ regex

必要な正規表現は /instructions([^<]+)/ のようなものです。これは、< 文字で終わっていることを前提としています。

于 2009-12-11T04:44:20.300 に答える