ruby - nokogiri + mechanize css selector by text -


I am new to nogogiri and so far I am most familiar with CSS selectors, I have to parse the information from the table below Trying to get a sample of the table and I am using, if I am stuck on the appropriate statement, because it is returning the entire contents of the table.

Table:

  & lt; Div class = "holder" & gt; & Lt; Div class = "row" & gt; & Lt; Div class = "c1" & gt; & Lt ;! - I do not need content - & gt; & Lt; / Div & gt; & Lt; Div square = "c2" & gt; & Lt; Span class = "data" & gt; & Lt ;! - I do not need stuff - & gt; & Lt; Span class = "data" & gt; & Lt; / Div & gt; & Lt; / Div & gt; ... & lt; Div class = "row" & gt; & Lt; Div class = "c1" & gt; Specific text & lt; / Div & gt; & Lt; Div square = "c2" & gt; & Lt; Span class = "data" & gt; What i want & lt; / Span & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; My script: (If specific text is found in the table, it gives each "div.c2 span.data" variable - so I either either my loop or my knowledge Has spoiled the statement)  
  data = [] page.agent.get (url) page.search ('div.row'). Do each. Row_data | If (row_data.search ('div.c1: in ("/ specific text /").). Text.strip temp = row_data.search (' div.c2 span.data '). Text.strip data & lt; & Lt; end of the temporary end  

There is no need to stop and insert the ruby ​​logic when you can remove it from a CSS The selector is required.

  data = page. Search ('div.row'; div.c1: in it ("specific text") + div.c2 span.data ')  

This will include only those who match the selector (e.g.

Your argument may be wrong here:

this code

  if (row_data.search ('div.c1: in it ( "Specific text") '... temp = row_data.search (' div.c2 span.data ') ...  

first searches the row for specific text, then If it matches, returns all the rows that match the second query, which has the same starting point. Key + In the CSS selector above, which will follow the immediate elements. (E.g. Next brother element). I am definitely making a assumption that the next element is always at your will.


Comments