URLを抽出するために、私は以下を使用しています:
html = open('http://lab/links.html')
urls = URI.extract(html)
これはうまくいきます。
<br >
ここで、タグの間にあるプレフィックス http または https のない URL のリストを抽出する必要があります。http または https タグがないため、URI.extract は機能しません。
domain1.com/index.html<br >domain2.com/home/~john/index.html<br >domain3.com/a/b/c/d/index.php
プレフィックスのない URL はそれぞれ<br >
タグの間にあります。
<TD> と <SPAN> 内の <BR> の後のテキストを取得するために、この Nokogiri Xpath を見てきましたが、機能しませんでした。
出力
domain1.com/index.html
domain2.com/home/~john/index.html
domain3.com/a/b/c/d/index.php
中間ソリューション
doc = Nokogiri::HTML(open("http://lab/noprefix_domains.html"))
doc.search('br').each do |n|
n.replace("\n")
end
puts doc
残りの HTML タグ ( !DOCTYPE, html, body, p
) を削除する必要があります...
解決
str = ""
doc.traverse { |n| str << n.to_s if (n.name == "text" or n.name == "br") }
puts str.split /\s*<\s*br\s*>\s*/
ありがとう。