rony36
rony36

Reputation: 3339

Parsing with Nokogiri

I'm parsing HTML using Nokogiri, and then getting these type elements.

<li data-item="{"title":"where is title","slug":"about some",
    "has_many_images":false,"show_image":"abbxb","created_at":1373737401,
    "show_attr":{"value":"150"},
    "location":"Alabama",
    "category":"Table",
    "is_business":false}">

    //here other many more
</li>

Now I want to get this data-item, I'm using:

 page.css("li[data-item]")[0]

I'm getting something like this:

#<Nokogiri::XML::Element:0x14fc250 name="li" attributes=[#<Nokogiri::XML::Attr:0x14fc178 name="class" value="item">, and so on ...

But I want like this:

"{"title":"where is title","slug":"about some",
        "has_many_images":false,"show_image":"abbxb","created_at":1373737401,
        "show_attr":{"value":"150"},
        "location":"Alabama",
        "category":"Table",
        "is_business":false}"

Any suggestion?

Upvotes: 1

Views: 198

Answers (1)

Jacob Brown
Jacob Brown

Reputation: 7561

You can get that attribute with the following selection:

page.at_xpath("//li[1]/@data-item").content

EDIT

A more complete demonstration, at @Priti's request:

body = %Q{     
  <body>
    <li data-item='{"title":"where is title","slug":"about some",
      "has_many_images":false,"show_image":"abbxb","created_at":1373737401,
      "show_attr":{"value":"150"},
      "location":"Alabama",
      "category":"Table",
      "is_business":false}'>
    </li>
  </body>
}
page = Nokogiri::XML(body)
result = page.at_xpath("//li[1]/@data-item").content
# "{\"title\":\"where is title\",\"slug\":\"about some\",         \"has_many_images\":false,\"show_image\":\"abbxb\",\"created_at\":1373737401,         \"show_attr\":{\"value\":\"150\"},         \"location\":\"Alabama\",         \"category\":\"Table\",         \"is_business\":false}"

Upvotes: 2

Related Questions