Working with Elements

Once we’ve selected the elements that are appropriate for the task at hand, using either XPath or CSS selectors, our job is most likely only half done. We typically want to actually do something with the elements we’ve matched.

There are two types of things we might want to do with the elements. The first set of tasks involves extracting information—either the text of elements within the set or the contents of attributes on those elements. Exactly where the information is stored depends both on the elements we’re dealing with and what we’re trying to do. If we’ve matched an image, the interesting part might be the src attribute, specifying the URL of the image; if we’ve matched a paragraph, then we’re likely to be interested ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.