0

I am writing a script that scrapes ad listings off a website called Gumtree(Australian Craigslist) using Nightmare.js and jQuery and I am getting an error:

(node:15902) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: $ is not defined

I was wondering if the website needs a reference like this:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

for me to scrape it with jQuery.

Aidan el Goste
  • 383
  • 1
  • 3
  • 23
  • Let me know if you need me to provide anymore information/my code – Aidan el Goste Apr 13 '18 at 01:37
  • Mate, this might be helpful. check this one. https://stackoverflow.com/questions/2194992/jquery-is-not-defined – Bee Apr 13 '18 at 02:05
  • @BikashPrajapati That isn't gonna help unfortunately, I just need to know if a website(that I don't own/have a way to edit the html e.g. ebay) needs to have that `jQuery` reference – Aidan el Goste Apr 13 '18 at 02:13
  • What happens when you try? Add jQuery to your own website and try to scrape it, then you can answer your own question. – Lansana Camara Apr 13 '18 at 02:26
  • No. You are using nightmare to make a request and get the document jquery is just being used as a dom selector tool. I recommend against jquery in this case it's 30 something kb you really don't need you can use document.querySelector and querySelectorAll with no issues in a nodejs environment. – Daniel Tate Apr 13 '18 at 02:33

1 Answers1

0

I'm not familiar with Nightmare.js, but wrote a few scraper and just run in the Chrome's console. If the website doesn't have jQuery, you can add it with the following script:

var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
jq.onload = jq.onreadystatechange = function() {
    if (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') {
        console.log("jQuery loaded");
        jQuery.noConflict();
    };
};
Adam Fentosi
  • 1,198
  • 10
  • 10