Last active
August 29, 2015 14:10
-
-
Save thash/8273e206a36199ebfa5a to your computer and use it in GitHub Desktop.
AllAbout 024 HTML(1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nokogiri::XML::Node#at メソッドは検索結果の最初のヒットを返す | |
doc.at('title') | |
# => #(Element:0x3feb0890f99c { name = "title", children = [ #(Text "All About(オールアバウト)")] }) | |
doc.at('title').class # => Nokogiri::XML::Element |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nokogiri::XML::Node#css, および Nokogiri::XML::Node#xpath は | |
# Elementのまとまりである「Nokogiri::XML::NodeSet」を返す | |
doc.css('ul#sys-topwaku-topix li') | |
doc.xpath('//ul[@id="sys-topwaku-topix"]/li') | |
# Nokogiri::XML::NodeSetはEnumerableなので、eachやmapで各Elementにアクセスできる | |
doc.css('ul#sys-topwaku-topix li').each do |li| | |
puts "[#{li.text}](#{li.at(:a).attr(:href)})" | |
end | |
# => [稼げる旦那、判断のためのポイント5つ](http://allabout.co.jp/matome/cl000000005902/) | |
# [あなたが運動しても痩せない理由](http://allabout.co.jp/newsdig/c/74939) | |
# [就職に有利な使える資格ランキング](http://allabout.co.jp/matome/cl000000005395/) | |
# [独身者よりカップルの方が長生きする?](http://allabout.co.jp/gm/gc/301665/) | |
# [「拝見させていただく」はNG敬語](http://allabout.co.jp/matome/cl000000005866/) | |
# [大阪で見たい「おもろい看板」7選](http://allabout.co.jp/matome/cl000000005637/) | |
# [恋人との問題、見て見ぬふりで大丈夫?](http://allabout.co.jp/gm/gc/449062/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'bundler/setup' | |
Bundler.require | |
doc = Nokogiri::HTML(open('http://allabout.co.jp/').read) | |
doc.class # => Nokogiri::HTML::Document | |
doc.class.ancestors | |
# => [Nokogiri::HTML::Document, | |
# Nokogiri::XML::Document, | |
# Nokogiri::XML::Node, # Nodeを継承している | |
# Enumerable, | |
# Nokogiri::XML::PP::Node, | |
# Object, PP::ObjectMixin, Kernel, BasicObject] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
puts open('http://allabout.co.jp/').read # => WebページのHTMLをStringとして取得 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
html = open('http://allabout.co.jp/').read | |
# 正規表現 | |
puts html.scan(/<title>.*<\/title>/) #=> <title>All About(オールアバウト)</title> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment