So I am parsing a URL and want to get a list of all the links in a page using Nokogiri.
But I want to push the results returned into a two-dimensional array.
I am now doing this:
def my_list(url)
root = Nokogiri::HTML(open(url))
list = []
root.css("a").each do |link|
list << (link[:href])
end
end
This gives me just the http links. If I do list << link it gives me the full <a> tag.
What I want to do is to push just the text of the link (can use link.text) to say list[0][0], and then the href value (using link[:href]) to the other cell say list[0][1].
How do I do that?
Thanks.
def my_list(url)
root = Nokogiri::HTML(open(url))
root.css("a").map do |link|
[link.text, link[:href]]
end
end
def my_list(url)
root = Nokogiri::HTML(open(url))
list = []
root.css("a").each do |link|
list << [link.text,link[:href]]
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With