6

Hi complete newbie here so bear with me. Seems like a simple job but I can't seem to find an easy way to do this.

So I need to extract a particular text from a webpage "www.example.com/index.php". I know that the text would be available in p tag with certain id. How do I extract this data out using javascript?

What I'm trying currently is that I have my javascript file (trying.js) on my computer with the following code:

$(document).ready(function () {
    $.get("www.example.com/index.php", function(data) {
        console.log(data)
    }) ;
});

and a html that runs the javascript file.

When I open this html page with firefox it doesn't show me anything in console. How do I get the website's data? Am I on the correct track here? Is there a better way to do this?

Vivek
  • 153
  • 1
  • 2
  • 9
  • 11
    You can't, javascript has a same origin policy, so you don't have access to other websites than those on the same domain or services that support JSONP or CORS. – adeneo Oct 04 '13 at 13:03
  • 1
    possible duplicate of [Can Javascript read the source of any web page?](http://stackoverflow.com/questions/680562/can-javascript-read-the-source-of-any-web-page) – Blazemonger Oct 04 '13 at 13:06
  • You need to write an app, maybe using `Selenium` or `Watin` browser automation or my new favorite CSQuery (it has only read access to the DOM but uses JQuery style filters in CSharp and is really fast). – iCollect.it Ltd Oct 04 '13 at 13:23
  • @adeneo client-side javascript that is the case, JavaScript runs in many different places, for example node.js does not have browser access-policy restrictions. – balupton Dec 30 '15 at 06:37
  • @balupton - I sort of assumed it was clientside, as the OP was specifically using jQuery and Firefox, but I could be wrong. – adeneo Dec 30 '15 at 14:01
  • @adeneo yeah no worries, was just clarifying the use of JavaScript there. – balupton Jan 05 '16 at 21:39
  • What you're looking for is a page scraper. Javascript can't pull it off because it can only gather data from the domain you're on. You could build it in Ruby, for example, and use one of the many existing gems for this sort of task, like https://github.com/assaf/scrapi or http://nokogiri.org/ – Orlando Oct 04 '13 at 13:34
  • Please take a look at https://stackoverflow.com/questions/680562/can-javascript-read-the-source-of-any-web-page There are multiple ways discussed. Hope it helps you. – Dropout Oct 04 '13 at 13:43

0 Answers0