Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  20] [ 1]  / answers: 1 / hits: 18493  / 12 Years ago, sat, january 26, 2013, 12:00:00

I want to to get data within divs from other websites.
How can I do that using JavaScript?


More From » jquery

 Answers
30

Due to cross domain restrictions you can't access the html directly using AJAX.


You can however use Yahoo YQL to select the part(s) of a page you want and have that html returned within jsonp data.


Example returning list of questions on main page of stackoverflow


var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url='http://stackoverflow.com/' and xpath='//div[@id="question-mini-list"]//h3//a'&format=json&callback=?';


$.getJSON( url, function(data){
$.each(data.query.results.a, function(){
$('body').append('<div><a href="http://stackoverflow.com'+this.href +'">'+this.content+'</a></div>')
})
})

DEMO: http://jsfiddle.net/NTUx5/


YQL docs: http://developer.yahoo.com/yql/guide/index.html


EDIT: This does not work any more.


[#80607] Friday, January 25, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ashly

Total Points: 601
Total Questions: 95
Total Answers: 88

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
;