繁体   English   中英

Chrome扩展如何从url获取页面源代码

[英]Chrome extension How to get source code of page from url

我想从 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 object,您可以在其中展示内容

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM