Introducing how to extract HTML meta tags and meta keywords used for SEO using Nokogiri, a scraping library in the Ruby programming language.
doc = Nokogiri::HTML(page.body)
doc.at("meta[name='keywords']")['content']
# or
doc.xpath('//meta[@name="keywords"]/@content').text
The above program is executed in the following steps:
- Parse HTML with the Nokogiri::HTML method
- Get the element with the meta tagโs name attribute being keywords using the at method
- Get the value of the content attribute
Also, to get the element with the meta tagโs name attribute being description, use the following:
doc.at("meta[name='description']")['content']