0

I am fetching html from a website. I want to get specific html from that page not all how to get that? I have tried following;

before appending data to target below

container.html(data);

I want to do like data.find('.site-header').html(); and then do container.html(data);

How can I achieve that?

DEMO

HTML

<div id="target"></div>

Script

$(function () {
    var container = $('#target');
    var url = 'http://aamirshahzad.net';

    $.getJSON("http://query.yahooapis.com/v1/public/yql?" +
        "q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent(url) +
        "%22&format=xml'&callback=?",

    function (data) {
        if (data.results[0]) {
            var data = filterData(data.results[0]);
            container.html(data);
        } else {
            var errormsg = '<p>Error: could not load the page.</p>';
            container.html(errormsg).focus().effect('highlight', {
                color: '#c00'
            }, 1000);
        }
    });

});


function filterData(data) {
    // filter all the nasties out
    // no body tags
    data = data.replace(/<?\/body[^>]*>/g, '');
    // no linebreaks
    data = data.replace(/[\r|\n]+/g, '');
    // no comments
    data = data.replace(/<--[\S\s]*?-->/g, '');
    // no noscript blocks
    data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
    // no script blocks
    data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
    // no self closing scripts
    data = data.replace(/<script.*\/>/, '');
    // [... add as needed ...]
    return data;
}
1
  • You call the getJSON function asking for XML (format=xml) and get a HTML response? Commented Aug 13, 2015 at 14:44

2 Answers 2

2

Quite simply;

container.html($(data).find('.site-header'));

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah Indeed sometime it won't work with no reason :D Thanks
1

Add this line after container.html(data);:

container.html(container.find(".site-header"));

Here is your updated JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.