Home > Software design >  space in classname is forcing table to be unselectable
space in classname is forcing table to be unselectable

Time:01-08

when scraping the forexfactory calendar, there is a space in the <table > and so nokogiri is not able to find the table.

Nokogiri::CSS::SyntaxError (unexpected '$' after '  ')

require "nokogiri"
require "csv"
require "open-uri"
url1="https://www.forexfactory.com/calendar?day=Jan5.2023"
html = URI.open(url1)
doc = Nokogiri::HTML(html)
table = doc.at('calendar__table  ')

The table exists in this html as see in this screenshot. So how do i scrape this table? enter image description here

CodePudding user response:

Node#at takes either a CSS selector or an XPath string. Since you're trying to match a CSS class, you should use a CSS selector.

table = doc.at('.calendar__table')

For completeness, you can also do it with XPath, but you have to tell XPath about the spaces in the class in that case, since it's viewing the document as raw XML, not as HTML with CSS classes.

table = doc.at('//*[@]')
  • Related