How can I scrape data from string like this
<script type="application/json" class="js-react-on-rails-component" data-component-name="ItemPriceHeading">
{
"price":"29.0",
"discountedPrice":null,
"offerPrice":null,
"totalPrice":"29.0",
"currency":"PLN"
}
</script>
I need to scrape "price" and "currency" values, but I can't understand how to do it.
I can scrape all strings, but how do I extract only selected parameters?
You can just select the <script>
tag with cheerio and then get the text and parse it like json:
Here is an example:
const cheerio = require("cheerio");
const $ = cheerio.load(
`<script type="application/json" class="js-react-on-rails-component" data-component-name="ItemPriceHeading">{"price":"29.0","discountedPrice":null,"offerPrice":null,"totalPrice":"29.0","currency":"PLN"}</script>`
);
const myJSON = JSON.parse(
$('script[data-component-name="ItemPriceHeading"]').text()
);
console.log(myJSON);
myJSON variable should be equal to:
{
price: '29.0',
discountedPrice: null,
offerPrice: null,
totalPrice: '29.0',
currency: 'PLN'
}