【问题标题】:Parsing JSON-LD Microformat from HTML using node.js?使用 node.js 从 HTML 解析 JSON-LD 微格式?
【发布时间】:2021-06-09 06:07:44
【问题描述】:

我正在尝试从 Autodesk 商店网页 (example) 上的 Microformat JSON-LD 数据中提取评级。目的是使用 badgen.net 制作评级徽章。但是,我无法让任何解析器提取 JSON-LD 数据。

我在 Runkit 上使用 node.js 来提取和解析元数据。 https://runkit.com/thomasa88/60c049de4daa38001add07ca

我尝试过使用 microformat-node 和 microformats-parser,但它们似乎都没有在页面上找到 JSON-LD 数据。如何正确提取数据?

在下面的 sn-p 中,我提取了相关的 HTML 并尝试将其提供给 microformats-parser:

response = {}
response.body = `<html><body>    <script type="application/ld+json">
        {
        "@context": "http://schema.org/",
        "@type": "SoftwareApplication",
        "name": "ParametricText",
        "image": "https://autodesk-exchange-apps-v-1-5-staging.s3.amazonaws.com/data/content/files/images/JLH9M8296BET/2114937992453312456/resized_930d9014-acf1-4a47-809b-a2373cf161f0_.png?AWSAccessKeyId=AKIAJGORQX2SECMU24IQ&amp;Expires=1623357670&amp;response-content-disposition=inline&amp;response-content-type=image%2Fpng&amp;Signature=T0SHZ2bodYMOIIgZDgc%2BWdWW2mU%3D",
        "operatingSystem": "Win64",
        "applicationCategory": "http://schema.org/DesktopApplication",
            "aggregateRating":{
            "@type": "AggregateRating",
            "ratingValue": "5",
            "ratingCount": "4"
            },
        "offers":{
        "@type": "Offer",
        "price": "0",
        "priceCurrency": "USD"
        }
        }
    </script></body></html>`


const { mf2 } = require("microformats-parser");

const parsed = mf2(response.body, {
  baseUrl: "https://apps.autodesk.com"
});

console.log(parsed);

结果:

{"rels":{},"rel-urls":{},"items":[]}

【问题讨论】:

    标签: node.js json-ld microformats runkit.com


    【解决方案1】:

    感谢this answer,我想出了以下解决方案。

    是喜欢 superagent 还是 got - 我不知道 :) .

    const superagent = require('superagent');
    const cheerio = require('cheerio');
    
    /*const response = await superagent("https://apps.autodesk.com/FUSION/en/Detail/Index?id=2114937992453312456&appLang=en&os=Win64");
    Gives response.text*/
    
    var got = require("got");
    var response = await got("https://apps.autodesk.com/FUSION/en/Detail/Index?id=2114937992453312456&appLang=en&os=Win64");
    
    const $ = cheerio.load(response.body);
    const jsonRaw = $("script[type='application/ld+json']")[0].children[0].data; 
    const result = JSON.parse(jsonRaw);
    //console.log(result);
    console.log(result.aggregateRating.ratingValue);
    

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2016-12-25
      • 2017-09-25
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多