Jump to content

Get data from external website


zozoa

Recommended Posts

Hello all,

I'm trying to create a web page that user will insert a URL of external site's web page and then the user gets some data from the specific web page.

 

I've read it can be done using jQuery, using the load function, but it doesn't work cross-domain.

 

Here is how my page looks like:

 

Untitled.png

 

I will appreciate any help,

thanks.

Link to comment
Share on other sites

That's called scraping.

 

What's your code so far?

No code so far, this is the page target.

I wrote the code that inject the data I want once I have the page data (I tested it by adding script to the required web page).

 

That's correct, default browser settings will not allow you to send ajax requests to a different domain, protocol, or port. If you want to do that you'll need to use a server-side proxy to get the data and send it back to Javascript.

Can you explain how do I do that?

I'm quite newbie in web developing.

Edited by zozoa
Link to comment
Share on other sites

PHP, for example, can send a request to any other server to get the HTML for a page. So you would have Javascript send the ajax request to a PHP file on your own server, which is allowed because it's on the same domain and everything, and Javascript sends the URL that it wants to look up. PHP gets the URL, sends the request for that page, gets the data, and sends the data back to Javascript as the response to the ajax request. Then you have the page data in Javascript.

Link to comment
Share on other sites

Hey,

Thanks for that.

 

So now I have an HTML page and a PHP page:

 

Z.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script type="text/javascript">$(document).ready(function(){  $("#Go").click(function(){	$("#div1").load("proxy.php #yw1"); });});</script></head><body><p>Please insert URL:</p><input id="url" type="text" size="100"><br><br><button type="button" id="Go">Go</button><div id="div1">Data will be displayed here</div></body></html>

proxy.php

<?php    $url = 'http://www.transfermarkt.com/zlatan-ibrahimovic/detaillierteleistungsdaten/spieler/3455';    $htm = file_get_contents($url);    echo $htm;?>

I want to pass the text from <input id="url" ...> to $url on proxy.php.

I've read it can be done by passing the control to proxy.php.

Is it possible to do it but staying on Z.html?

Link to comment
Share on other sites

Learn about PHP form handling in order to get data from inputs to PHP: http://www.w3schools.com/php/php_forms.asp

 

If you don't want to load a different page you can use AJAX to make Javascript receive the data instead. Learn AJAX: http://www.w3schools.com/ajax/default.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...