Extracting data from a script using XPath

Viewed 55

I do not manage to extract the thumbnail link with xpath in the following script/json:

<script type="application/ld+json">{"@context":"https://schema.org","@type":"VideoObject","name":"XYZ","description":"Text","thumbnailUrl":"https://www.thumbnail.com/thumb.jpg","uploadDate":"2000-01-01","contentUrl":"https://www.example.com"}</script>

I need the thumbnail link (https://www.thumbnail.com/thumb.jpg) after "thumbnailUrl", but do not know how the selector would follow after

//script[@type="application/ld+json"]/...

Please help.

1 Answers

when the you have a file name "script.html" with the content:

<script type="application/ld+json">{"@context":"https://schema.org","@type":"VideoObject","name":"XYZ","description":"Text","thumbnailUrl":"https://www.thumbnail.com/thumb.jpg","uploadDate":"2000-01-01","contentUrl":"https://www.example.com"}</script>

then:

xidel -s -e "//script[1]"  script.html  >temp.json
xidel -s -e '$json."thumbnailUrl"' temp.json

should output:

https://www.thumbnail.com/thumb.jpg

tested with: Xidel 0.9.8 (on Windows)

EDIT:

It is also possible in one step

xidel -s -e "json(//script)/thumbnailUrl"  script.html
Related