简体   繁体   English

Chrome DevTools。 自动将控制台输出保存到文件

[英]Chrome DevTools. Save console output to file automatically

I try to get text content from the webpage. 我尝试从网页获取文本内容。 For example Google.com 例如Google.com

I write at console: 我在控制台上写道:

$ ('#SIvCob').innerText

and get: 并获得:

"Google offered in: русский"

This is the text, what I find out. 这是我所发现的文字。 Now I want to save it to file (.txt). 现在,我想将其保存到文件(.txt)。

Two moments: there is no only one item, that I search, actually 7-10. 两分钟:我搜索的不仅只有一项,实际上是7到10。 And, there is a refresh every second! 而且,每秒刷新一次! I go to write a cycle. 我去写一个循环。

I know about copy() function and about right click on the console and "Save As," but I need a CODE, which will do it automatically. 我知道copy()函数,以及在控制台和“另存为”上单击鼠标右键,但是我需要一个CODE,它将自动执行此操作。

Thanks in advance. 提前致谢。

The browser has no API to write to the file system since that would be a security risk. 浏览器没有API可以写入文件系统,因为这会带来安全风险。 But you can use Nodejs and their File System API to write you text file. 但是您可以使用Node.js及其文件系统API来编写文本文件。

You will also need to use the HTTP API to get the web content. 您还需要使用HTTP API来获取Web内容。 And you will also need to parse your HTML, you can do it with fast-html-parser or any other module of your choice. 而且,您还需要解析HTML,您可以使用fast-html-parser或您选择的任何其他模块来解析HTML。 (high5, htmlparser, htmlparser2, htmlparser2-dom, hubbub, libxmljs, ms/file, parse5, ...) (high5,htmlparser,htmlparser2,htmlparser2-dom,hubbub,libxmljs,ms / file,parse5等)

 var http = require('http');
 var fs = require('fs');
 var parser = require('node-html-parser');
 var options = {
   host: 'www.google.com',
   port: 80,
   path: '/index.html'
 };
 var file = '/path/to/myFile.txt';
 http.get(options, function(res) {
   res.setEncoding('utf8');
   var body = ''; 
   res.on('data', function (chunk) {body += chunk});
   res.on('end', function () { 
     var dom = parser.parse(body);
     var text = dom.querySelector('#SIvCob').text;
     fs.writeFile(file, text, function (err) {
       if (err) throw err;
       console.log('The file has been saved!');
     });
   });
 });

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

相关问题 如何使用 WebStorm 在 Chrome DevTools 控制台中显示 TypeScript 文件 output 行号? - How to show TypeScript file output line number in Chrome DevTools Console with WebStorm? 如何检查chrome中devtools源代码的console.log输出 - how to check the console.log output of devtools source code in chrome 网站检测到 devtools。 代码附在网站来源的 DevToolDectect.js 文件中 - Website detects devtools. Code is attached of DevToolDectect.js file from the website sources Chrome Devtools控制台无法在某些网站中使用 - Chrome Devtools console not working in certain websites Chrome DevTools 控制台文本突出显示颜色为黑色 - Chrome DevTools Console Text Highlight Color is Black CORS-状态200,但Chrome devtools控制台错误 - CORS - Status 200 but error in Chrome devtools console 避免检测“Chrome DevTools(console)是否打开” - Avoid the detection of “whether Chrome DevTools(console) is open” 在 Chrome devtools 控制台中运行 javascript fetch 时出错 - Error running javascript fetch in Chrome devtools console Chrome devtools 控制台不显示关闭 - Chrome devtools console does not display closure 一种重置Chrome DevTools控制台上下文的方法 - A way to reset Chrome DevTools console's context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM