user1626730
user1626730

Reputation: 4113

How do I get the contents within HTML tags of certain attributes (e.g., colspan) using the Ruby gem Mechanize?

Here's an example of the HTML:

<td colspan="3">TARGET</td>
<td>NOT TARGET</td>

How do I use Ruby's Mechanize gem to target only the <td> with colspan="3"?

Upvotes: 0

Views: 74

Answers (1)

the Tin Man
the Tin Man

Reputation: 160571

Mechanize relies on the HTML/XML parser Nokogiri to handle its heavy lifting.

Typically we use agent as the variable for Mechanize. Testing against a local gem server, this will get an instance of a parsed document, and allow me to extract nodes and their parameters:

page = agent.get('http://0.0.0.0:8808/')
page.at('a')

If I want the text content of that node:

page.at('a').text
# => "abstract"

In your case, use page.at('td[colspan="3"]').text to get the contents.

Upvotes: 1

Related Questions