[英]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.