简体   繁体   English

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

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

I want to download source code of page from url in Chrome extension.我想从 Chrome 扩展中的 url 下载页面的源代码。

I have something like this.我有这样的东西。 But don't know how to format text to html.但不知道如何将文本格式化为 html。 Or if this formatting works how to display the source code even in console.或者,如果这种格式有效,如何在控制台中显示源代码。

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

For example I would like to get text from span "dataValue".例如,我想从跨度“dataValue”中获取文本。 How can I achieve that?我怎样才能做到这一点?

在此处输入图像描述

Your wording makes it hard to guess what you want你的措辞让人很难猜到你想要什么

The first console.log is the textual representation第一个 console.log 是文本表示

The second is the DOM object where you can get stuff to show第二个是 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