したがって、配列要素を循環しています。これが返される結果です。
[nil, [#<Nokogiri::XML::Element:0x835386d4 name="a" attributes=[#<Nokogiri::XML::Attr:0x835385f8 name="href" value="http://bham.craigslist.org/web/2961573018.html">] children=[#<Nokogiri::XML::Text:0x835381c0 "Web Designer Full time">]>
私がやりたいのはhref
、値にアクセスしてから値にアクセスすることtext
です。それ、どうやったら出来るの?
私はこれを試しました:
puts i[:href]
しかし、それはこのエラーを生成します:
TypeError: Symbol as array index
ちなみに、私は次のi
ように each を介して配列内の要素としてアクセスしています。
contents.each do |i|
puts i.inspect
puts i[:href]
end
編集1:
これがcontents
配列を生成する方法です。混乱する可能性があるため、名前を変更する必要はありません:)
contents = {}
first_items.each do |link|
content_url = link
content_page = Nokogiri::HTML(open(content_url))
contents[link[:href]] = content_page.css("p a")
end
puts contents.inspect
これは出力を取得するものです:
{nil=>[#<Nokogiri::XML::Element:0x85fee914 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fee838 name="href" value="http://bham.craigslist.org/web/2961573018.html">] children=[#<Nokogiri::XML::Text:0x85fee400 "Web Designer Full time">]>, #<Nokogiri::XML::Element:0x85fee298 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fee1bc name="href" value="http://bham.craigslist.org/web/2959813303.html">] children=[#<Nokogiri::XML::Text:0x85fedd84 "Once in a lifetime opportunity...">]>, #<Nokogiri::XML::Element:0x85fedc1c name="a" attributes=[#<Nokogiri::XML::Attr:0x85fedb40 name="href" value="http://bham.craigslist.org/web/2925485723.html">] children=[#<Nokogiri::XML::Text:0x85fed708 "Website Designer and Blogging Internship!">]>, #<Nokogiri::XML::Element:0x85fed5a0 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fed4c4 name="href" value="http://bham.craigslist.org/web/2918424652.html">] children=[#<Nokogiri::XML::Text:0x85fed08c "Excellent Java Developer Opportunity!">]>, #<Nokogiri::XML::Element:0x85fecf24 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fece48 name="href" value="http://bham.craigslist.org/web/2888669703.html">] children=[#<Nokogiri::XML::Text:0x85feca10 "Freelance Graphic Design">]>, #<Nokogiri::XML::Element:0x85fec8a8 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fec7cc name="href" value="http://bham.craigslist.org/web/2900256461.html">] children=[#<Nokogiri::XML::Text:0x85fec394 "GWT/GXT Developer">]>, #<Nokogiri::XML::Element:0x85fec22c name="a" attributes=[#<Nokogiri::XML::Attr:0x85fec150 name="href" value="http://bham.craigslist.org/web/2897641463.html">] children=[#<Nokogiri::XML::Text:0x85febd18 "Website hiring!">]>]}
の出力の完全な値は次のi
とおりです。
--------------------
This is the value of i:
[nil, [#<Nokogiri::XML::Element:0x85fee914 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fee838 name="href" value="http://bham.craigslist.org/web/2961573018.html">] children=[#<Nokogiri::XML::Text:0x85fee400 "Web Designer Full time">]>, #<Nokogiri::XML::Element:0x85fee298 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fee1bc name="href" value="http://bham.craigslist.org/web/2959813303.html">] children=[#<Nokogiri::XML::Text:0x85fedd84 "Once in a lifetime opportunity...">]>, #<Nokogiri::XML::Element:0x85fedc1c name="a" attributes=[#<Nokogiri::XML::Attr:0x85fedb40 name="href" value="http://bham.craigslist.org/web/2925485723.html">] children=[#<Nokogiri::XML::Text:0x85fed708 "Website Designer and Blogging Internship!">]>, #<Nokogiri::XML::Element:0x85fed5a0 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fed4c4 name="href" value="http://bham.craigslist.org/web/2918424652.html">] children=[#<Nokogiri::XML::Text:0x85fed08c "Excellent Java Developer Opportunity!">]>, #<Nokogiri::XML::Element:0x85fecf24 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fece48 name="href" value="http://bham.craigslist.org/web/2888669703.html">] children=[#<Nokogiri::XML::Text:0x85feca10 "Freelance Graphic Design">]>, #<Nokogiri::XML::Element:0x85fec8a8 name="a" attributes=[#<Nokogiri::XML::Attr:0x85fec7cc name="href" value="http://bham.craigslist.org/web/2900256461.html">] children=[#<Nokogiri::XML::Text:0x85fec394 "GWT/GXT Developer">]>, #<Nokogiri::XML::Element:0x85fec22c name="a" attributes=[#<Nokogiri::XML::Attr:0x85fec150 name="href" value="http://bham.craigslist.org/web/2897641463.html">] children=[#<Nokogiri::XML::Text:0x85febd18 "Website hiring!">]>]]
--------------------
This is the value of i.href:
編集2:
ところで、これは実際の HTML 出力がどのように見えるかです...私はこれをしました:
builder = Nokogiri::HTML::Builder.new do |doc|
doc.html {
doc.body {
contents.each do |el|
if !el.nil?
puts "-" * 20
puts "This is the value of el: "
puts el.inspect
puts "-" * 20
puts "This is the value of el.href: "
puts el[:href]
end
doc.p {
doc.a el, :href => el
}
end
}
}
end
puts "*" * 50
puts "This is the HTML generated"
puts builder.to_html
これはどのように見えるかです:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p><a href="<a%20href=%22http://bham.craigslist.org/web/2961573018.html%22>Web%20Designer%20Full%20time</a><a%20href=%22http://bham.craigslist.org/web/2959813303.html%22>Once%20in%20a%20lifetime%20opportunity...</a><a%20href=%22http://bham.craigslist.org/web/2925485723.html%22>Website%20Designer%20and%20Blogging%20Internship!</a><a%20href=%22http://bham.craigslist.org/web/2918424652.html%22>Excellent%20Java%20Developer%20Opportunity!</a><a%20href=%22http://bham.craigslist.org/web/2888669703.html%22>Freelance%20Graphic%20Design</a><a%20href=%22http://bham.craigslist.org/web/2900256461.html%22>GWT/GXT%20Developer</a><a%20href=%22http://bham.craigslist.org/web/2897641463.html%22>Website%20hiring!</a>"><a href="http://bham.craigslist.org/web/2961573018.html">Web Designer Full time</a><a href="http://bham.craigslist.org/web/2959813303.html">Once in a lifetime opportunity...</a><a href="http://bham.craigslist.org/web/2925485723.html">Website Designer and Blogging Internship!</a><a href="http://bham.craigslist.org/web/2918424652.html">Excellent Java Developer Opportunity!</a><a href="http://bham.craigslist.org/web/2888669703.html">Freelance Graphic Design</a><a href="http://bham.craigslist.org/web/2900256461.html">GWT/GXT Developer</a><a href="http://bham.craigslist.org/web/2897641463.html">Website hiring!</a></a></p></body></html>