node.js - 使用node.js从HTML解析JSON-LD微格式?

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

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

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

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

在下面的代码片段中,我提取了相关的 HTML 并尝试将其提供给微格式解析器:

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":[]}

最佳答案

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

是喜欢 super 代理还是得到 - 我不知道:)。

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

关于node.js - 使用node.js从HTML解析JSON-LD微格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67898468/

相关文章:

node.js - 处理浏览器重新加载 socket.io

javascript - 如何修复 'Cannot read property ' 的 'null' id'

node.js - MongoDB closeSphere 只返回 100 个结果?

java - 使用Jena编写JSON-LD

javascript - 禁用链接和 <abbr> 上的浏览​​器工具提示

html - Google 微格式应该在网页上可见吗?

javascript - Redis 在某些操作系统上将字符串保存为缓冲区,而不是其他操作系统?

Json-LD > 定义一个 "person"以便于作为 WebPage 模式的不同键上的值重用

google-search - 处理客户端付费专区的 isAccessibleForFree