2016-11-07 219 views
0

我想從url獲取數據:autotrader_url 不幸的是,我無法處理重定向。 這是我的代碼到目前爲止。在NodeJs中處理重定向請求

var request = require('request'); 
var cheerio = require('cheerio'); 

request({followAllRedirects: true,url:"http://www.autotrader.com/cars-for-sale/showcase.xhtml?zip=94536&endYear=2017&Log=0&modelCode1=LEGACY&sortBy=derivedpriceDESC&startYear=1981&makeCode1=SUB&numRecords=25&searchRadius=25&mmt=%5BSUB%5BLEGACY%5B%5D%5D%5B%5D%5D&makeCodes=SUB"}, 
function (error, response, html) { 
    console.log(response) 
    if (!error && response.statusCode == 200) { 
    console.log("yo"); 
    var $ = cheerio.load(html); 
    console.log($("title").text()); 
    $('div.listing-title h2').each(function(i, element){ 
     var a = $(this);  
     console.log(a.innerHTML);  
     }); 
    } 
}); 

我在想什麼?

回答

0

followAllRedirects: true選項將遵循從服務器發回的http重定向。看起來他們沒有使用http重定向,因爲當您在瀏覽器中訪問該頁面時,它會加載一個頁面,其中顯示「我們正在搜索您想要的汽車」,並且該頁面在瀏覽器中使用javascript重定向。要遵循這種重定向,您可能必須使用類似phatomjs的東西。

或者使用cheerio(也許)或正則表達式的某種組合,您可以直接從源獲取重定向url,然後在您擁有正確的url後自行向該URL發出第二個請求。

<script type="text/javascript"> 
    $(function() { 
     atc.create(atc.showcaseRedirect, { 
      destinationUrl: '/cars-for-sale/Subaru/Legacy/Fremont+CA-94536?endYear=2017&firstRecord=0&makeCode1=SUB&mmt=%5BSUB%5BLEGACY%5B%5D%5D%5B%5D%5D&modelCode1=LEGACY&searchRadius=25&showcaseOwnerId=68619541&startYear=1981&Log=0', 
      queryString: '', 
      .... 
     }).init(); 
    }); 
</script> 

你只需要抓住那個destinationUrl。現在就這麼說吧,這裏假設你沒有違反任何使用條款,所以在你繼續前進之前,你一定要考慮一下。

我不知道,如果它是一個錯誤在他們結束,或者如果他們試圖阻止人們刮,但你需要設置一個User-Agent頭,讓他們作出迴應所以這添加到您的請求。

這是一個完整的工作示例:

var request = require('request'); 
var cheerio = require('cheerio'); 
var firstUrl = "http://www.autotrader.com/cars-for-sale/showcase.xhtml?zip=94536&endYear=2017&Log=0&modelCode1=LEGACY&sortBy=derivedpriceDESC&startYear=1981&makeCode1=SUB&numRecords=25&searchRadius=25&mmt=%5BSUB%5BLEGACY%5B%5D%5D%5B%5D%5D&makeCodes=SUB"; 

makeRequest(firstUrl, function(err, html) { 
    if(err) { 
    return console.log('There was a problem'); 
    } 
    // get "redirect" url from page source 
    var re = new RegExp("destinationUrl\:[^\,\}]*"); 
    var redirectUrl = 'http://www.autotrader.com' + html.match(re)[0].replace('destinationUrl: ', '').replace('\'', ''); 
    console.log('redirectUrl', redirectUrl); 
    // make the second request and process the markup with cheerio 
    makeRequest(redirectUrl, processFinalMarkup); 
}); 

function processFinalMarkup(err, html) { 
    var $ = cheerio.load(html); 
    console.log($("title").text()); 
    $('div.listing-title h2').each(function(i, element){ 
    var a = $(this);  
    console.log(a.innerHTML);  
    });  
} 

function makeRequest(url, callback) { 
    request({ 
    // Their page requires a User-Agent to be set. 
    headers: { 
     'User-Agent': 'express' 
    }, 
    followAllRedirects: true, 
    url: url 
    }, 
    function (error, response, html) { 
     console.log(response.headers, response.statusCode); 
    if (!error && response.statusCode == 200) { 
     console.log("yo"); 
     callback(null, html); 
    } 

    }); 
}