Javascript access another webpage
Asked Answered
F

3

9

I know very, very little of javascript, but I'm interested in writing a script which needs information from another webpage. It there a javascript equivalent of something like urllib2? It doesn't need to be very robust, just enough to process a simple GET request, no need to store cookies or anything and store the results.

Flamboyant answered 23/7, 2010 at 3:44 Comment(0)
U
9

There is the XMLHttpRequest, but that would be limited to the same domain of your web site, because of the Same Origin Policy.

However, you may be interested in checking out the following Stack Overflow post for a few solutions around the Same Origin Policy:


UPDATE:

Here's a very basic (non cross-browser) example:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/questions/3315235', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4)  { 
    console.log(xhr.responseText);
  }
};
xhr.send(null);

If you run the above in Firebug, with Stack Overflow open, you'd get the HTML of this question printed in your JavaScript console:

JavaScript access another webpage http://img217.imageshack.us/img217/5545/fbugxml.png

Uncharted answered 23/7, 2010 at 3:48 Comment(1)
Thank you, it looks like AJAX will suit my needs agreeably.Flamboyant
C
0

You could issue an AJAX request and process it.

Carrigan answered 23/7, 2010 at 3:46 Comment(0)
P
0

Write your own server, which runs the script to load the data from websites. Then from your web page, ask your server to fetch the data from websites and send them back to you.

see http://www.storminthecastle.com/2013/08/25/use-node-js-to-extract-data-from-the-web-for-fun-and-profit/

Pilgarlic answered 18/12, 2014 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.