Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Scraper Question #88

Closed
nrc0004 opened this issue Mar 23, 2017 · 4 comments
Closed

Web Scraper Question #88

nrc0004 opened this issue Mar 23, 2017 · 4 comments

Comments

@nrc0004
Copy link

nrc0004 commented Mar 23, 2017

We are building a web scraper and it works the first time you deploy it, but then the node server crashes and it cant be used again. Below is our code and the error we get:

const express = require("express")
const cheerio = require("cheerio")
const request = require("request")
const fs  = require('fs')

const app = express()

app.get('/', function (req, res){

  url = 'https://ucr.fbi.gov/crime-in-the-u.s/2015/crime-in-the-u.s.-2015/tables/table-1'


  request(url, function(error, response, html){
    if(!error){
      var $ = cheerio.load(html)
      var population
      var json = { population: ""}

      $('#cell51').filter(function(){
        var data = $(this)
        population = data.html()

        json.population = population;
        res.send(population)
      })
    }
  })
})

app.listen('4000')
console.log('Scraping faces on 4k')

exports = module.exports = app

Error:

[nodemon] starting node index.js
Scraping faces on 4k
_http_outgoing.js:367
throw new Error('Can't set headers after they are sent.');
^

Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:367:11)
at ServerResponse.header (/Users/natashaclark/wdi/Lab/sweet-libs/node_modules/express/lib/response.js:725:10)
at ServerResponse.send (/Users/natashaclark/wdi/Lab/sweet-libs/node_modules/express/lib/response.js:170:12)
at Object. (/Users/natashaclark/wdi/Lab/sweet-libs/index.js:24:13)
at testFn (/Users/natashaclark/wdi/Lab/sweet-libs/node_modules/cheerio/lib/api/traversing.js:320:22)
at /Users/natashaclark/wdi/Lab/sweet-libs/node_modules/lodash.filter/index.js:869:9
at /Users/natashaclark/wdi/Lab/sweet-libs/node_modules/lodash.filter/index.js:1255:11

@amaseda
Copy link
Contributor

amaseda commented Mar 23, 2017

Hm, is the .filter callback executing multiple times? If so, running res.send( ) might be what's causing that error.

@nrc0004
Copy link
Author

nrc0004 commented Mar 23, 2017 via email

@nrc0004
Copy link
Author

nrc0004 commented Mar 23, 2017 via email

@amaseda
Copy link
Contributor

amaseda commented Mar 23, 2017

Oh word, awesome!

@amaseda amaseda closed this as completed Mar 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants