Reputation: 609
I have Nokogiri parsing this ESPN website. I want to search the page for the "North Carolina" and return the rank immediately left of it, which is "1" in this case. I will do this for all teams in my database, but one example should solve it for me.
I'm not sure if XPath or CSS selectors would be best. The span with the rank of North Carolina is
//*[@id="content"]/div[3]/div[2]/div[1]/div[1]/dl[1]/dd[1]/div[1]/span
How would I search for "North Carolina" and return its corresponding span.rank
?
One potential wrinkle is, I'd need the syntax to account for any situation in which there are two teams on a single bracket line, e.g. "11 Georgia" versus "11 Rhode Island". The rank XPaths for those are slightly different than above, span[1]
for the first team and span[2]
for the second team:
//*[@id="content"]/div[3]/div[2]/div[2]/div[1]/dl[3]/dd[1]/div[2]/span[1]
//*[@id="content"]/div[3]/div[2]/div[2]/div[1]/dl[3]/dd[1]/div[2]/span[2]
Upvotes: 0
Views: 74
Reputation: 89285
One possible XPath to get span
element that corresponds to a
element containing certain text :
//div[@class='team' and a='NORTH CAROLINA']/span
Upvotes: 1