javascript - 如何写入文本文件Javascript?
问题描述
我正在尝试将 webScrape 写入文本文件,但它不起作用。文本文件一遍又一遍地说是对象。任何帮助将不胜感激,我对这种类型的编程相对较新,而且我是空白的!
编辑:我现在可以使用文本文件,但我想知道如何在添加每行数据后换行。
const axios = require('axios');
const cheerio = require('cheerio');
const camelCase = require('camelcase'); // added this for readale properties
const fs = require('fs')
// use async / await feature
async function scrape(url) {
// get html page
const { data } = await axios.get(url);
// convert html string to cheerio instance
const $ = cheerio.load(data);
// query all list items
return $('.tabular-data-panel > ul')
// convert cheerio collection to array for easier manipulation
.toArray()
// transform each item into proper key values
.map(list => $(list)
// query the label element
.find('.panel-row-title')
// convert to array for easier manipulation
.toArray()
// use reduce to create the object
.reduce((fields, labelElement) => {
// get the cheerio instance of the element
const $labelElement = $(labelElement);
// get the label of the field
const key = $labelElement.text().trim();
// get the value of the field
const value = $labelElement.next().text().trim();
// asign the key value into the reduced object
// note that we used camelCase() to make the property easy to read
fields[camelCase(key)] = value;
// return the object
return fields;
}, {})
);
}
async function main() {
const url = 'https://www.lseg.com/resources/1000-companies-inspire/2018-report-1000-companies-uk/search-1000-companies-uk-2018?results_per_page=100';
const companies = await scrape(url);
fs.writeFile('Output.txt', companies, (err) => {
if (err) throw err;
console.log('it/s done')
})
console.log(companies);
}
main();
解决方案
函数的结果scrape(url)
似乎就像一个数组。你可以JSON.stringify
把结果变成字符串。或者,您也可以只使用join()
// ...
const companies = await scrape(url).then(data => JSON.stringify(data))
或者
const companies = await scrape(url).then(data => data.join('\n'))
推荐阅读
- flutter - 在颤动中单击相机按钮后手电筒重新启动
- reactjs - 当 id 未知时,CRA + react-intl 崩溃
- python - 如何获取手部图像中点的坐标?
- php - 如何定义 wp-cron?
- c# - OAuth 1.0 GetAuthorizationHeader - WinForm C#
- python - music21:写入midi文件后和弦转音符?
- python - 将 Miniconda 的 Python 更新到 3.9.x,但 Miniconda 是随 Homebrew 安装的
- .net-core - 重复的“System.Reflection.AssemblyCompanyAttribute”属性 (CS0579)
- java - 在 Android Studio 中使用警报管理器设置多个警报会导致问题
- machine-learning - 准确度值在训练过程中上下波动