Problem with scraping make my trip flight data using cheerio

Viewed 244

I am scraping Make My Trip Flight data for a project but for some reason it doesn't work. I've tried many selectors but none of them worked. On the other hand I also tried scraping another site with the same logic, and it worked. Can someone point out where did I go wrong?

I am using cheerio and axios

const cheerio = require('cheerio');
const axios = require('axios');

Make My Trip:

axios.get('https://www.makemytrip.com/flight/search?itinerary=BOM-DEL-14/11/2020&tripType=O&paxType=A-1_C-0_I-0&intl=false&cabinClass=E').then(urlRes => {
    const $ = cheerio.load(urlRes.data);
    $('.fli-list.one-way').each((i, el) => {
        const airway = $(el).find('.airways-name ').text();
        console.log(airway);
    });
}).catch(err => console.log(err));

The other site for which the code works:

axios.get('https://arstechnica.com/gadgets/').then(urlRes => {
    const $ = cheerio.load(urlRes.data);
    $('.tease.article').each((i, el) => {
        const link = $(el).find('a.overlay').attr('href');
        console.log(link);
    });
}).catch(err => console.log(err));
2 Answers

TLDR you should parse

  • https://voyager.goibibo.com/api/v2/flights_search/find_node_by_name_v2/?search_query=DEL&limit=15&v=2

instead of

  • https://www.makemytrip.com/flight/search?itinerary=BOM-DEL-14/11/2020&tripType=O&paxType=A-1_C-0_I-0&intl=false&cabinClass=E

Explanation (hope it is clear enough)

Cause you're trying to parse heavy web application using one plain GET request ... it is impossible in this way :) The main difference between provided urls:

  • the second web page (yes just a page not js app like makemytrip) 'https://arstechnica.com/gadgets/' respond to you with a complete content

  • makemytrip respond to you only with a js script, which do the work - loads data and etc.

To parse such complicated web apps you should investigate (press f12 in browser -> web) all requests which are running in your browser on page load and repeat these requests in your script ... like in this case you could notice API endpoint which responds with all needed data.

I think cheerio works just fine, I will recommend go over the HTML again and find a new element, class or something else to search for.

When I went into the given url I did not find .fli-list.one-way in any combination.

Just try to find something more particular to filter on.

If you still need help I can try and scrape it by myself and send you some code

Related