Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract data from a website using javascript.

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?

like image 563
Vivek Avatar asked Oct 04 '13 13:10

Vivek


People also ask

How do I scrape data from a website using JavaScript?

First, install Cheerio and Axios by running the following command: npm install cheerio axios . Then create a new file called crawler. js and copy/paste the following code: const axios = require('axios'); const cheerio = require('cheerio'); const getPostTitles = async () => { try { const { data } = await axios.

Can JavaScript be used for web scraping?

Web scraping with JavaScript is a very useful technique to extract data from the Internet for presentation or analysis.

Can I fetch data from database using JavaScript?

Because php is a server side language it supports our connection execution so when our server request success then we can fetch any data from database. In javascript we achieve this by ajax. Ajax has XMLHttpRequest object to communicate with servers.


2 Answers

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/

like image 81
Orlando Avatar answered Sep 23 '22 21:09

Orlando


Please take a look at Can Javascript read the source of any web page?

There are multiple ways discussed. Hope it helps you.

like image 29
Dropout Avatar answered Sep 23 '22 21:09

Dropout