尝试抓取

时间:2016-08-27 23:29:31

标签: javascript ajax xmlhttprequest

我试图通过点击下载按钮使用api来使用javascript抓取网页。但是我没有收到任何响应文本,并且使用curl也可以使用相同的api。

download.addEventListener('click',function(){

    document.getElementById('check').innerHTML = url;

    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=json&callback=?';  
    document.getElementById('check').innerHTML = yql;
    var request = new XMLHttpRequest();


    request.onreadystatechange = function(){
        document.getElementById('check').innerHTML= request.readyState;

        if(request.readyState ===XMLHttpRequest.DONE) {
        document.getElementById('check').innerHTML= request.responseText;
       }
    };

    request.open("GET", yql, true);

    request.send;

},false);

1 个答案:

答案 0 :(得分:-1)

只需从ypl中删除回调,然后此代码就可以正常工作:

download.addEventListener('click',function(){

    document.getElementById('check').innerHTML = url;


    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=jsonp';  
    document.getElementById('check').innerHTML = yql;


    var request = new XMLHttpRequest();
    request.open("GET", yql, true);   
    request.send();  

    request.onreadystatechange = function(){
        document.getElementById('check').innerHTML= request.readyState + request.status;
        if(request.readyState ===4) {
            document.getElementById('check').innerHTML= request.responseText;
        }
    };


},false);