我想从 Chrome 扩展程序中的 url 下载页面的源代码。
我有这样的事情。但不知道如何将文本格式化为 html。或者,如果这种格式有效,即使在控制台中也能显示源代码。
fetch('https://www.transfermarkt.com/robert-lewandowski/profil/spieler/38253').then(r => r.text()).then(result => {
// Result now contains the response text, do what you want...
console.log("Fetch result: " + result);
var parser = new DOMParser();
var source_code = parser.parseFromString(result, "text/html");
console.log("Source code: " + source_code);
});
例如,我想从跨度“dataValue”中获取文本。我怎样才能做到这一点?
你的措辞让人很难猜到你想要什么
第一个 console.log 是文本表示
第二个是 DOM 对象,您可以在其中获取要显示的内容
fetch('https://www.transfermarkt.com/robert-lewandowski/profil/spieler/38253').then(r => r.text()).then(result => {
// Result now contains the response text, do what you want...
// console.log("Source code: " + result); // textual representation
var parser = new DOMParser();
var DomObject = parser.parseFromString(result, "text/html");
console.log("Name: " + DomObject.querySelector("title").textContent);
[...DomObject.querySelectorAll(".dataItem")].forEach(item => {
if (item.textContent.trim() === "Joined:") {
console.log("Joined:",item.nextElementSibling.textContent);
}
});
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句