How do I extract text at even positions?

Bala

I am extracting a page with Nokogiri which returns a Nokogiri::XML::NodeSet. From this, I would like to extract only the text from even nodes.

doc.search("h2 a").class #=> Nokogiri::XML::NodeSet
doc.search("h2 a").count #=> returns 148

I am interested in 0,2,4,8, etc.:

doc.search("h2 a")[0].text #=> the one I wanted.
doc.search("h2 a")[2].text #=> the one I wanted.
Arup Rakshit

Try the below :

doc.search("h2 a").map.with_index(0) do |i,nd|
    i.text if nd.even?
end.compact

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I output the array's elements (on odd positions, that have an odd amount of even digits?

How do I check that even positions of a string array have only numbers?

How do I extract text after <i class> tag?

How do I extract the number before a text with variable spacing in r?

How do I extract a section number and the text after it?

xpath: How do I extract text within the "strong" tag?

How do I extract quoted portions from a text in perl?

How do I extract text from awk results?

How do I extract a list of Windows services and their status to a text file?

BeautifulSoup: How do I extract the text child element with no tag?

How do I extract specific parts of text from a string in R?

How do I extract value from a text file in Makefile?

How do I extract text that lies between two indicators?

How do I extract text between two characters in R

How do I extract text from a URL path in R?

How do I extract text fragments of a file using sed?

How do I extract the text content of an article site with Python 3?

How do I extract text between two words in Google Sheets?

How do I extract each words from a text file in scala

How do I extract words from a list in a text in R?

How do I extract text and numbers from a string within a cell?

How do I extract text between second quotation and first comma?

How do i extract text from email body using UiPath?

How do I extract only number from dynamic headline/text?

How do I extract a text from a website(html)

how to position images in even positions

How do I extract all digits from a string in Python even if the digits are not contiguous?

How do i extract the image extension from URL even if some URL has no extension?

How can I extract text from single quotes, even if the text itself contains single quotes, using regex in Python?