Chrome扩展程序如何从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 对象,您可以在其中获取要显示的内容

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Chrome扩展程序:获取活动选项卡的源代码

如何使用fetch在Firefox或chrome网页扩展程序中获取网页的HTML源代码?

如何通过扩展程序在Chrome DevTools源代码编辑器中获取光标位置?

如何从Chrome扩展程序中的网站获取完整的HTML源代码

从Chrome扩展程序获取当前页面的源HTML

如何通过Chrome扩展程序获取页面的Google Analytics(分析)跟踪ID?

如何通过Chrome扩展程序获取特定页面的会话数据?

整个页面的源代码,包括chrome扩展名中的框架

如何从Chrome扩展程序调用页面的javascript函数?

如何在不刷新Chrome页面的情况下查看HTML源代码

Chrome扩展程序:在页面呈现/加载之前获取页面的html

使用 JavaScript 获取特定页面的源代码

如何从Chrome扩展程序中打开的窗口获取背景页面?

如何通过获取HTML页面源代码

单击chrome扩展程序图标时,在新窗口中打开当前页面的URL

如何更改页面的源代码?使用硒和java

如何使用thymeleaf在页面的源代码中打印mensagens?

如何在Chrome扩展程序中通过鼠标调整POPUP.html页面的大小

如何读取当前页面的HTML以访问Chrome扩展程序中的元素?

Chrome 扩展程序:如何获取新创建的标签 URL

如何从Google Chrome扩展程序获取当前标签的URL?

如何在Google Chrome扩展程序中获取基本URL

Chrome扩展程序保留对弹出页面的更改

Chrome扩展程序的背景页面的快捷键?

Netbeans Chrome扩展源代码

如何获取页面的绝对URL?

使用Crossrider从扩展中获取当前页面的url

复制整个页面的html源代码

如何在我自己的扩展程序中获取刚打开的页面而不是 chrome 特殊页面