从同一网页抓取多个网址

时间:2019-01-31 10:19:52

标签: javascript html css node.js

我正在尝试创建一个从网站产品中获取所有信息的刮板,到目前为止,我已经疯狂到可以获取我想要的所有网址,并从我想要的html页面中获取信息,问题是我现在必须组合这两个代码...而我无法弄清楚。

这将获得我想要的所有链接:

const rp = require('request-promise');
const $ = require('cheerio')
const url = 'https://www.shogun.nl/airsoft/pistolen-296.html';
 
 
rp(url)
  .then(function(html){
    //
    const urls = [];
    for (let i = 0; i < 84; i++) {
      urls.push($('h2 > a', html)[i].attribs.href);
    }
    console.log(urls);
  })
  .catch(function(err){
    //handle error
  });

这将获得单个页面的信息:

const rp = require('request-promise');
const $ = require('cheerio');
const url = 'https://www.shogun.nl/airsoft/pistolen-296/glock-19.html';
 
const Parse = function(url) {
  return rp(url)
    .then(function(html) {
      return{
        productName: $('.product-name > h1', html).text(),
        prijs: $('.product-shop > .price-box-wrap > .f-left > .price-box > .regular-price > .price', html).text(),
        merk: $('.merk > .attr_val', html).text(),
        airsoftkaliber: $('.airsoftkaliber > .attr_val', html).text(),
        airsoftsysteem: $('.airsoftsysteem > .attr_val', html).text(),
        fps: $('.fps > .attr_val', html).text(),
        joule: $('.joule > .attr_val', html).text(),
        schietmodus: $('.schietmodus > .attr_val', html).text(),
        magazijncapaciteit: $('.magazijncapaciteit > .attr_val', html).text(),
        lengteproduct: $('.lengteproduct > .attr_val', html).text(),
        voorzienvanrail: $('.voorzienvanrail > .attr_val', html).text(),
        gewicht: $('.gewicht > .attr_val', html).text(),
      };
    })
    .catch(function(err){
      //handle error
    });
};
//functie om alle specificaties op te slaan om later door te spelen door ze een naam te geven.
//hierdoor zijn ze makkelijk op te roepen op een webpagina.
module.exports = Parse;
 
rp(url)
 
  .then(function(html) {
    console.log($('.product-name > h1', html).text());
    console.log($('.product-shop > .price-box-wrap > .f-left > .price-box > .regular-price > .price', html).text());
    console.log($('.merk > .attr_val', html).text());
    console.log($('.airsoftkaliber > .attr_val', html).text());
    console.log($('.airsoftsysteem > .attr_val', html).text());
    console.log($('.fps > .attr_val', html).text());
    console.log($('.joule > .attr_val', html).text());
    console.log($('.schietmodus > .attr_val', html).text());
    console.log($('.magazijncapaciteit > .attr_val', html).text());
    console.log($('.lengteproduct > .attr_val', html).text());
    //lengteproduct in mm
    console.log($('.voorzienvanrail > .attr_val', html).text());
    console.log($('.gewicht > .attr_val', html).text());
  })
 
  .catch(function(err) {
    //handle error
  });
//dit print in de console de selectie. kan later dus weg.

但是要获得我从上面的代码中获得的网址并将其插入到底部代码中,这对我来说就像是魔幻的.....

0 个答案:

没有答案