Ruby, Nokogiri: Get the element name of the selected node

Introduction

In Nokogiri, you can select a node by specifying the element name with css or at_css, but on the contrary, I was wondering how to get the element name from the selected node, so I checked it. ..

use the name method

Getting the element name of a node is very simple. Suppose you have the following html file.

hello.html


<html>
  <head>
    <title>hello</title>
    <meta charset="UTF-8">
  </head>
  <body>
    <p>Hello</p>
  </body>
</html>

Select the p tag in at_css.

sample.rb


require 'nokogiri'

html = open('hello_utf8.html').read
doc = Nokogiri::HTML.parse(html)

element = doc.at_css('p')

p element.name  #=> 'p'
p element.parent.name  #=> 'body'

You can also use parent etc. to select another node and get the element name.

at the end

Normally, the html structure is first looked at and then scraped, so I don't think there is much demand for getting the element name from the node ^^;

Recommended Posts

Ruby, Nokogiri: Get the element name of the selected node
[Kotlin] Get the argument name of the constructor by reflection
Get the name of the test case in the JUnit test class
Get the URL of the HTTP redirect destination in Ruby
Get the object name of the instance created by the new operator
Ruby / rexml: Turn each with an element with the same name
About the behavior of ruby Hash # ==
[Ruby] See the essence of ArgumentError
Get the ID of automatic numbering
[For beginners] How to get the Ruby delayed railway line name
[Ruby] Display the contents of variables
[Ruby] Specify the argument name of the method. Meaning of the colon (:) at the end
Get data of all Premier League players by scraping with Ruby (nokogiri)
Get the type of an array element to determine if it is an array
I want to get the field name of the [Java] field. (Old tale tone)
Get the result of POST in Java
part of the syntax of ruby ​​on rails
[Swift] Get the height of Safe Area
[Ruby] Cut off the contents of twitter-ads
Ruby from the perspective of other languages
[Rails] Change the label name of f.label
[Swift] Change the color of SCN Node
graphql-ruby: How to get the name of query or mutation in controller Note
[Rails] Get the path name of the URL before the transition and change the link destination
Call a method of the parent class by explicitly specifying the name in Ruby
How to find the cause of the Ruby error
[Ruby] Summary of class definitions. Master the basics.
[Ruby on Rails] Until the introduction of RSpec
When you get lost in the class name
The story of introducing Ajax communication to ruby
[Java] Get the length of the surrogate pair string
[JAVA] Get only the file name, excluding the extension
Manage the version of Ruby itself with rbenv
How to get today's day of the week
Rename the package name of the existing jar file
[Ruby] Code to display the day of the week
To get Ruby Silver at the last minute
The official name of Spring MVC is Spring Web MVC
[Java] How to get the authority of the folder
I checked the number of taxis with Ruby
I want to get the value in Ruby
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java