【问题标题】:NodeJS program to scrape table columns from WikipediaNodeJS 程序从 Wikipedia 中抓取表格列
【发布时间】:2021-11-08 23:05:21
【问题描述】:

我正在学习 Node JS 以及如何进行网页抓取。我认为这是从维基百科页面中提取列的好方法https://en.wikipedia.org/wiki/List_of_S%26P_500_companies

我一直在学习使用 Cheerio 进行网络抓取,但不确定如何在 NodeJS 中编写代码。我已经熟悉 html 选择器来识别页面上的元素,但不知道如何提取到程序中。我计划将这些信息提取到一个列表中。我希望在 wiki 页面的表格中提取符号和安全列。

下面是我编译的代码和我得到的结果。我根据网页上的选择器创建了一个 const。我相信它应该根据选择器返回列中的所有值。

var AWS = require("aws-sdk");
var AWS = require("aws-sdk/global");

AWS.config.apiVersions = {
  dynamodb: '2012-08-10'
};

var dynamodb = new AWS.DynamoDB();
const cheerio = require('cheerio');
const axios = require('axios');
const express = require('express');

async function getStocks() {
  try {
    const url = ' https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'

    const { data } = await axios({
      method: "GET",
      url: url,
    })

    const $ = cheerio.load(data)
    const elemSelector = '#constituents > tbody > tr:nth-child(1) > td'

    $(elemSelector).each((parentIdx, parentElem) =>  {
      console.log(parentIdx)
    })
    console.log($)
  } catch (err) {
    console.error(err)
  }
}

getStocks()

结果

[Function: initialize] {


html: [Function: html],
  xml: [Function: xml],
  text: [Function: text],
  parseHTML: [Function: parseHTML],
  root: [Function: root],
  contains: [Function: contains],
  merge: [Function: merge],
  load: [Function: load],
  _root: Node {
    type: 'root',
    name: 'root',
    parent: null,
    prev: null,
    next: null,
    children: [ [Node], [Node] ],
    'x-mode': 'no-quirks'
  },
  _options: { xml: false, decodeEntities: true },
  fn: Cheerio { constructor: [Function: LoadedCheerio] }
}
[Finished in 2.384s]

【问题讨论】:

    标签: node.js cheerio


    【解决方案1】:

    这应该会有所帮助。跟随 cmets 了解正在发生的事情。您没有指定您想要的数据输出方式,因此我将它们作为数组提供,但您可以添加代码以将表数据与表头映射,因为您拥有原始数据。

    // parses HTML
    var $ = cheerio.load(data);
    // the number of columns you want to target on the table, starting from the left column
    var number_of_columns = 2;
    // targets the specific table with a selector
    var html_table = $('table#constituents');
    // gets table header titles; loops through all th data, and pulls an array of the values
    var table_header = html_table.find('th').map(function() {return $(this).text().trim();}).toArray();
    // removes columns after the number of columns specified
    table_header = table_header.slice(0, number_of_columns);
    // gets table cell values; loops through all tr rows
    var table_data = html_table.find('tbody tr').map(function(tr_index) {
      // gets the cells value for the row; loops through each cell and returns an array of values
      var cells = $(this).find('td').map(function(td_index) {return $(this).text().trim();}).toArray();
      // removes columns after the number of columns specified
      cells = cells.slice(0, number_of_columns);
      // returns an array of the cell data generated
      return [cells];
      // the filter removes empty array items
    }).toArray().filter(function(item) {return item.length;});
    // output the table headers
    console.log('table_header', table_header);
    // output the table data
    console.log('table_data', table_data);
    

    【讨论】:

      【解决方案2】:

      在 nodejs 中,您可以使用 puppeteer 进行网页抓取。这是一个非常好的库,有很多浏览器设置和查询选择器选项!

      您可以在 https://pptr.dev/ 上阅读文档,了解有关该库的更多信息

      如果您想将其与 API 集成,我也可以为您提供帮助。

      【讨论】:

      • 我集成了代码。 puppeteer 和 Cheerio 一样吗?
      猜你喜欢
      • 1970-01-01
      • 2019-04-20
      • 2016-08-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2020-04-19
      相关资源
      最近更新 更多