在 React Native 中,我想解析
www.google.com
并获取其具有类 class="lnXdpd"
的元素。
我不确定如何使用
querySelector
所以一旦我获取 url 并使用其字符串来解析它 querySelector
使用 Fetch API 获取 HTML 页面文件内容,然后使用 HTML Parser 进行解析。
import { parse } from "node-html-parser";
async function parseURL() {
try {
// Smithsonian Museum Homepage
const response = await fetch("https://www.si.edu/");
const html = await response.text();
const parsed = parse(html, {
blockTextElements: {
script: false,
noscript: true,
style: true,
pre: true,
},
});
// Find elements with form-details class
console.log(parsed.querySelector(".form-details")?.toString());
} catch (error) {
console.log(error);
}
}
parseURL()
演示 - https://replit.com/@EmmanuelBYIRING/html-parser-url#index.js