how can I scrap data from string like this
<script type="application/json" data-component-name="ItemPriceHeading">{"price":"29.0","discountedPrice":null,"offerPrice":null,"totalPrice":"29.0","currency":"PLN"}</script>
I need scrap "price" and "currency" values, but I cant understand how to do it. Thanks
I can scrap all string , but how extract only selected parameters
CodePudding user response:
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" 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'
}