>本教學將使用JavaScript的Cheerio圖書館從Wikipedia中提取獎項獎的電影,並將其儲存到CSV檔案中。
npm install cheerio axios
const url = 'https://en.wikipedia.org/wiki/List_of_Academy_Award%E2%80%93winning_films';
取得頁面的HTML,然後使用Cheerio來解析:axios
const { data: html } = await axios.get(url); const $ = cheerio.load(html); const theadData = []; const tableData = [];
>最後,擷取的資料是格式的,並使用
>將半olons儲存到CSV檔案中,將半olon作為定界符:$('tbody').each((i, column) => { const columnData = []; $(column).find('th').each((j, cell) => { columnData.push($(cell).text().replace('\n', '')); }); theadData.push(columnData); }); tableData.push(theadData[0]); $('table tr').each((i, row) => { const rowData = []; $(row).find('td').each((j, cell) => { rowData.push($(cell).text().trim()); }); if (rowData.length) tableData.push(rowData); });
>使用:fs.writeFileSync
運行腳本
const csvContent = tableData.map((row) => row.join(';')).join('\n'); fs.writeFileSync('academy_awards.csv', csvContent, 'utf-8');
檔案包含刮擦資料。
>node scraper.js
academy_awards.csv
以上是Wikipedia學院獎列表的JavaScript刮刀。的詳細內容。更多資訊請關注PHP中文網其他相關文章!