3

I'm trying out PhantomJS and wanted to use it to extract content from a webpage. However, I cannot inject jQuery using the following code.

console.log('SSL support = ', require('system').isSSLSupported);

var page = require('webpage').create();
console.log('page created');
page.open('https://www.google.com/#q=my+test+query', function() {
  console.log('page opened');
  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", function() {
    console.log('jQuery injected');
    phantom.exit();
  });
});

When I run the code, I see an error

C:\Users\royshi\SkyDrive\Developer\Crawler>phantomjs test.txt
SSL support =  true
page created
page opened
TypeError: 'null' is not an object (evaluating 'document.body.appendChild')

  http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js:1
^C
C:\Users\royshi\SkyDrive\Developer\Crawler>

My code is not too different from the example provided by PhantomJS. Wondering what could be wrong here.

EDIT:

It seems there is something wrong with the URL I used in the above example (i.e. "https://www.google.com/#q=my+test+query"). The code works for another URL: https://www.google.com/:

C:\Users\royshi\SkyDrive\Developer\Crawler>phantomjs test.txt
SSL support =  true
page created
page opened
jQuery injected

C:\Users\royshi\SkyDrive\Developer\Crawler>

Wondering what's wrong with the URL "https://www.google.com/#q=my+test+query".

Roy
  • 880
  • 1
  • 12
  • 27

1 Answers1

2

Answering your edit as you got your solution: The hashtag triggers a redirect. As I don't know PhantomJS I suspect you need to follow it manually like this gist suggests.

nietonfir
  • 4,797
  • 6
  • 31
  • 43
  • you were right about the hashtag - the code works perfectly fine if I change the URL to "https://www.google.com/search?q=my+test+query". Thank you! – Roy Apr 06 '14 at 21:23