2016-11-07 105 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); 
    } 

    }); 
}