javascript - 将纯 JS 控制台日志或变量保存到 CSV
问题描述
我需要您的专业知识如何将 console.log 输出或变量保存到 csv 文件中?请检查我下面的代码谢谢!
jQuery('.result-row').each(function(index, value) {
var name = jQuery(this).find('a.result-name span').text();
var occupation = jQuery(this).find('span.result-suffix.result-suffix-verified').text();
var occupationwospace = occupation.replace(/\s \s/g, '')
console.log(name + '\t' + occupationwospace );
});
解决方案
一种方法是使用Blob 对象
let csv = ""; // CSV Content will be placed here
jQuery('.result-row').each(function(index, value) {
let name = jQuery(this).find('a.result-name span').text();
let occupation = jQuery(this).find('span.result-suffix.result-suffix-verified').text();
let occupationwospace = occupation.replace(/\s \s/g, '')
csv += name + '\t' + occupationwospace + '\n'; // Append to CSV String variable
});
const blob = new Blob([csv], {type: "text/csv;charset=utf-8"});
const blobUrl = URL.createObjectURL(blob);
// Create a link to download the file
const link = document.createElement("a");
link.href = blobUrl;
link.download = "data.csv";
link.innerHTML = "Click here to download the file";
document.body.appendChild(link);
// link.click(); // Auto download
推荐阅读
- ruby-on-rails - Rails 6、React 和 Webpack 开发服务器的 CSP 错误
- python - 类型错误:__init__() 缺少 2 个必需的位置参数:子弹射击系统的“x”和“y”
- php - 如何提高 Symfony 序列化器的性能?
- sass - 如何使 Angular 材质选项卡透明?
- python - 在 SparkSubmitOperator conf 参数中使用字典
- class - Echo 或 sh 方法在共享库中的 jenkins 类中不起作用
- sql - 在 SQL Server 中根据指定的数字动态获取行号间隔
- javascript - JS:Date.parse() 行为意外
- cmake - 将 qmake 转换为 CMake
- python - 使用python工作和订购txt文件