问题
Was trying to understand the basics of Web Scraping & was successful doing it with Python. When trying to emulate the same with Node getting following error :
UnhandledPromiseRejectionWarning: Error: Max redirects exceeded.
Below is simple code snippet that am trying to run using node scrape.js :
const axios = require('axios');
const url = 'https://www.somewebsite.com/auth/get_menu/?city_id=1';
const headers = {
'accept': '*/*',
'content-type': 'application/json',
'app_client': 'consumer_web'
};
axios.get(url, {
headers,
maxRedirects: 0
})
.then(resp => {
console.log(resp.body);
})
.catch(error => {
if (error.response) {
console.log(error.response.data);
console.log('---------------------------------------------');
console.log(error.response.status);
console.log('---------------------------------------------');
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
NOTE : As pointed out in comments - the URL with those headers, does a 302 redirect to /skip_explore/?c=1&n=/auth/get_menu/?city_id=1 which then does a redirect to /auth/get_menu/?city_id=1 which is now a loop that lasts forever.
Please let me where am doing it wrong. Thanks in advance.
来源:https://stackoverflow.com/questions/59851293/node-js-simple-get-with-axios-module-throwing-error-max-redirects-exceeded