从`script`标签中使用Cheerio抓取数据

huangapple go评论63阅读模式
英文:

Scrape data from script tag by cheerio

问题

我如何从这样的字符串中提取数据

<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>

我需要提取"price"和"currency"的值,但我不知道如何做。

我可以提取所有字符串,但如何提取仅选择的参数?

英文:

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?

答案1

得分: 1

你可以使用cheerio仅选择<script>标签,然后获取文本并解析它,就像JSON一样:

这是一个示例:

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变量应该等于:

{
  price: '29.0',
  discountedPrice: null,
  offerPrice: null,
  totalPrice: '29.0',
  currency: 'PLN'
}
英文:

You can just select the &lt;script&gt; tag with cheerio and then get the text and parse it like json:

Here is an example:

const cheerio = require(&quot;cheerio&quot;);
const $ = cheerio.load(
    `&lt;script type=&quot;application/json&quot; class=&quot;js-react-on-rails-component&quot; data-component-name=&quot;ItemPriceHeading&quot;&gt;{&quot;price&quot;:&quot;29.0&quot;,&quot;discountedPrice&quot;:null,&quot;offerPrice&quot;:null,&quot;totalPrice&quot;:&quot;29.0&quot;,&quot;currency&quot;:&quot;PLN&quot;}&lt;/script&gt;`
);
const myJSON = JSON.parse(
    $(&#39;script[data-component-name=&quot;ItemPriceHeading&quot;]&#39;).text()
);
console.log(myJSON);

myJSON variable should be equal to:

{
  price: &#39;29.0&#39;,
  discountedPrice: null,
  offerPrice: null,
  totalPrice: &#39;29.0&#39;,
  currency: &#39;PLN&#39;
}

huangapple
  • 本文由 发表于 2023年1月8日 20:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75047827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定