ruipgil / scraperjs

A complete and versatile web scraper.
MIT License
3.7k stars 188 forks source link

how to use it in Angular #81

Open anuj9196 opened 6 years ago

anuj9196 commented 6 years ago

I want to use it in Angular 6 application.

In my component file, I have tried following code with your sample code.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-express',
  templateUrl: './express.component.html',
  styleUrls: ['./express.component.css']
})
export class ExpressComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    this.aliExpress();
  }

  aliExpress() {
    console.log('loaded ali express');

    var scraperjs = require('scraperjs');
    scraperjs.StaticScraper.create('https://news.ycombinator.com')
      .scrape(function ($) {
        return $('.title a').map(function () {
          return $(this).text();
        }).get();
      }).then(function (news) {
      console.log(news);
    });
  }

}

I also tried with

import {scraperjs} from 'scraperjs';

and

import scraperjs from 'scraperjs`;

But it is giving error as

Failed to compile.

./node_modules/cheerio/index.js 
Module not found: Error: Can't resolve './package' in '/home/user/code/angular/ali-express/node_modules/cheerio'