Tripadvisor Crawler Code

Use the code below to Crawl Tripadvisor for restaurant contact information.

Step 1 - Copy this actor into your Apify account ( create a free Apify account if you don't have one already)

Step 2 - Copy the code below into the "Page Function" section of the Apify task

Step 3 - On Tripadvisor, search for Restaurants in your target city or state. Copy the link into the Start URL section and click run.

Example Link -

async function pageFunction(context) {
    var $ = context.jQuery;
    var results = [];
    var base_url = ""

    //if page is a restaurant review, run the code below, otherwise
    // we're on a restaurant listing page
    if (context.request.url.includes("Restaurant_Review")){
        //email is in a mailto: format with ?subject at the end
        // clean this up before output
        var mail_string = $('').closest('a[href]').attr('href')
        var mail_parts = mail_string.split(':')
        var mail_parts2 = mail_parts[1].split('?')
        var mail = mail_parts2[0]

        // get the scheme from the script in the <head> section
        // this includes address info as well as restaurant name
        var schema = JSON.parse($('script[type="application/ld+json"]').first().text());
            email: mail,
            website : $('.ui_icon.laptop').closest('a[href]').attr('href'),
            telephone: $('').closest('span:not([class])').text().trim(),
            street: schema.address.streetAddress,
            postal: schema.address.postalCode,
            city: schema.address.addressLocality,

    //if we're on a restaurant listing page, for each restaurant listing
    // ad their review link to the scraping queue
        $('div[data-test*="list_item"]').each(function() {   
            var link = base_url.concat($(this).find('a').eq(0).attr('href'))
            context.enqueueRequest({ url : link })


        //add the link to go to the next page of results to the scraping queue
        var link2 = base_url.concat($(".next").attr('href'))
        context.enqueueRequest({ url : link2 })


    return results