2

I am trying to extract an image URL from a div, where the link to the file is stored as a json object in data-settings attribute:

<div class="c-offerBox_galleryItem">
    <div data-component="magnifier" data-component-on="@load" data-settings="{
                image: '/media/cache/gallery/rc/p2vgiqwd/images/42/42542/KRHE7Z29X19.jpg',
                ratio: 1.5,
                outside: 0
            }"></div>
</div>

Currently I can access data-settings with:

xidel "https://example.com" -e "//div[@class='c-offerBox_galleryItem']/div/@data-setting

The output is the json object. How can I access the image object?

I thought something like:

xidel "https://example.com" -e "//div[@class='c-offerBox_galleryItem']/div/@data-setting/$json/image

would work, but not.

Adrian
  • 2,576
  • 9
  • 49
  • 97
  • Judging by your use of quotes I guess you're a Windows user. I would add the `cmd`-tag in that case. – Reino Nov 30 '21 at 23:08

1 Answers1

2

No, you can only use the global default variable $json when "https://example.com" itself returns a JSON.
To parse a string as JSON use parse-json(). And in this case you'll need the option "liberal" as well.

xidel -s "https://example.com" -e "//div[@class='c-offerBox_galleryItem']/div/parse-json(@data-settings,{'liberal':true()})"

For Xidel 0.9.8 use json() (deprecated for newer builds).

xidel -s "https://example.com" -e "//div[@class='c-offerBox_galleryItem']/div/json(@data-settings)"
Reino
  • 3,203
  • 1
  • 13
  • 21
  • For some reason I am getting: err:XPST0017: unknown function: parse-json #1 – Adrian Dec 01 '21 at 19:11
  • @Adrian Then the binary you're using is too old. – Reino Dec 01 '21 at 22:57
  • I think this is the latest. $ xidel --version Xidel 0.9.8 (20180421.6162.1f357eaaf5f3) http://www.videlibri.de/xidel.html – Adrian Dec 02 '21 at 21:22
  • 1
    @Adrian No, xidel-0.9.9.20211123.8232.023d1f1f656e is the latest. http://www.videlibri.de/xidel.html: _"[Development snapshots](https://sourceforge.net/projects/videlibri/files/Xidel/Xidel%20development/) are provided in a special download folder occasionally."_ – Reino Dec 03 '21 at 12:27
  • OMG this was so hard to find, but now it works. Thanks. – Adrian Dec 03 '21 at 15:42
  • @Adrian Long time ago since I last used Xidel 0.9.8. See updated answer. – Reino Dec 03 '21 at 17:30