If I want to parse a HTML block using Nokogiri in Ruby like this:
<th class="first">ancd</th>
<th>xyz</th>
<th>sdf</th>
How do I exclude the tag containing a certain class name? the “first” in this case.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use CSS selectors:
In this simple case, you can also use xpath:
The difference is that xpath requires an exact match in the class name. If you had the possibility of having multiple classes, e.g.
<th class="red first">then the CSS selector will identify it, but the xpath won’t (without making it a bit more complicated).Edit: Just for reference, if you want the xpath that can select a class when there may be multiple classes on an element:
Normally XPath is a lot more flexible than CSS selectors, but this is an HTML edge case that favors CSS.