File size: 16,474 Bytes
65936f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GdqJu0iA0Qkm",
        "outputId": "a1d4fdd0-36f5-42de-f4a3-31fa124a0fb3"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m850.5/850.5 kB\u001b[0m \u001b[31m10.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.8/77.8 kB\u001b[0m \u001b[31m7.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h  Building wheel for google-search-results (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
          ]
        }
      ],
      "source": [
        "!pip install --upgrade google-search-results --quiet"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "XEzTZ14602JJ"
      },
      "outputs": [],
      "source": [
        "import serpapi\n",
        "import time\n",
        "serpapi_key=\"your key\""
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "most_visited_countries = [\n",
        "    \"France\", \"Spain\", \"United States\", \"China\", \"Italy\", \"United Kingdom\", \"Germany\", \"Mexico\", \"Thailand\", \"Turkey\",\n",
        "    \"Austria\", \"Malaysia\", \"Russia\", \"Greece\", \"Japan\", \"Canada\", \"Portugal\", \"Saudi Arabia\", \"Australia\", \"Netherlands\",\n",
        "    \"South Korea\", \"Egypt\", \"United Arab Emirates\", \"Singapore\", \"Indonesia\", \"India\", \"Vietnam\", \"Croatia\", \"Switzerland\",\n",
        "    \"Poland\", \"South Africa\", \"Sweden\", \"Morocco\", \"Philippines\", \"Argentina\", \"Brazil\", \"Belgium\", \"Denmark\", \"Norway\",\n",
        "    \"Peru\", \"Czech Republic\", \"Hungary\", \"Finland\", \"Ireland\", \"New Zealand\", \"Kenya\", \"Tanzania\", \"Israel\", \"Colombia\",\n",
        "    \"Chile\", \"Iran\", \"Qatar\", \"Oman\", \"Ukraine\", \"Jordan\", \"Romania\", \"Kazakhstan\", \"Pakistan\", \"Bangladesh\", \"Nepal\",\n",
        "    \"Bhutan\", \"Sri Lanka\", \"Myanmar\", \"Cambodia\", \"Laos\", \"Ethiopia\", \"Nigeria\", \"Kenya\", \"Uganda\", \"Rwanda\", \"Botswana\",\n",
        "    \"Zimbabwe\", \"Namibia\", \"Zambia\", \"Tunisia\", \"Algeria\", \"Mauritius\", \"Mauritania\", \"Senegal\", \"Ghana\", \"Ivory Coast\",\n",
        "    \"Cameroon\", \"Gabon\", \"Madagascar\", \"Seychelles\", \"Fiji\", \"Solomon Islands\", \"Vanuatu\", \"Samoa\", \"Tonga\", \"Kiribati\",\n",
        "    \"Tuvalu\", \"Marshall Islands\", \"Palau\", \"Micronesia\", \"Nauru\", \"Cook Islands\", \"Niue\", \"Tokelau\", \"Comoros\", \"Maldives\",\n",
        "    \"Cape Verde\", \"SΓ£o TomΓ© and PrΓ­ncipe\", \"Saint Kitts and Nevis\", \"Saint Lucia\", \"Saint Vincent and the Grenadines\",\n",
        "    \"Dominica\", \"Antigua and Barbuda\", \"Barbados\", \"Grenada\", \"Belize\", \"Suriname\", \"Guyana\", \"Paraguay\"\n",
        "]"
      ],
      "metadata": {
        "id": "1AvPO_JMlwEo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "hotels = []"
      ],
      "metadata": {
        "id": "BUvNcOk8pGLu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "oma2S1ai0QhM",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "a860bfc8-9949-489f-b6dd-7b2fa5ebb8da"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Total time for country: South Africa is 3.1462161540985107\n",
            "Total time for country: Sweden is 4.135618448257446\n",
            "Total time for country: Morocco is 3.870520830154419\n",
            "Total time for country: Philippines is 3.1940598487854004\n",
            "Total time for country: Argentina is 2.7493534088134766\n",
            "Total time for country: Brazil is 3.4907991886138916\n",
            "Total time for country: Belgium is 3.385352373123169\n",
            "Total time for country: Denmark is 5.446274518966675\n",
            "Total time for country: Norway is 2.5857763290405273\n",
            "Total time for country: Peru is 3.4917497634887695\n",
            "Total time for country: Czech Republic is 3.0693607330322266\n",
            "Total time for country: Hungary is 3.3840672969818115\n",
            "Total time for country: Finland is 3.126199722290039\n",
            "Total time for country: Ireland is 7.195910692214966\n",
            "Total time for country: New Zealand is 2.894592523574829\n",
            "Total time for country: Kenya is 3.255056858062744\n",
            "Total time for country: Tanzania is 2.524628162384033\n",
            "Total time for country: Israel is 5.516220808029175\n",
            "Total time for country: Colombia is 6.803558111190796\n",
            "Total time for country: Chile is 5.247052907943726\n",
            "Total time for country: Qatar is 6.248284339904785\n",
            "Total time for country: Oman is 2.459817886352539\n",
            "Total time for country: Ukraine is 2.5123510360717773\n",
            "Total time for country: Jordan is 3.0355286598205566\n",
            "Total time for country: Romania is 3.6406311988830566\n",
            "Total time for country: Kazakhstan is 3.4437084197998047\n",
            "Total time for country: Pakistan is 3.1847915649414062\n",
            "Total time for country: Bangladesh is 4.030826807022095\n",
            "Total time for country: Nepal is 2.4323649406433105\n",
            "Total time for country: Bhutan is 2.3996434211730957\n",
            "Total time for country: Sri Lanka is 4.682724952697754\n",
            "Total time for country: Myanmar is 2.983024835586548\n",
            "Total time for country: Cambodia is 2.5116500854492188\n",
            "Total time for country: Laos is 2.3876218795776367\n",
            "Total time for country: Ethiopia is 2.619466543197632\n",
            "Total time for country: Nigeria is 3.833418846130371\n",
            "Total time for country: Kenya is 0.1747727394104004\n",
            "Total time for country: Uganda is 3.2861359119415283\n",
            "Total time for country: Rwanda is 2.4624242782592773\n",
            "Total time for country: Botswana is 2.4395322799682617\n",
            "Total time for country: Zimbabwe is 2.4967572689056396\n",
            "Total time for country: Namibia is 3.7246780395507812\n",
            "Total time for country: Zambia is 2.8809072971343994\n",
            "Total time for country: Tunisia is -0.0001838207244873047\n",
            "Total time for country: Algeria is 3.2204713821411133\n",
            "Total time for country: Mauritius is 4.015950918197632\n",
            "Total time for country: Mauritania is 2.07582426071167\n",
            "Total time for country: Senegal is 2.4191691875457764\n",
            "Total time for country: Ghana is 1.9542012214660645\n",
            "Total time for country: Ivory Coast is 3.2751688957214355\n",
            "Total time for country: Cameroon is 2.822939872741699\n",
            "Total time for country: Gabon is 2.2693305015563965\n",
            "Total time for country: Madagascar is 3.0742127895355225\n",
            "Total time for country: Seychelles is 2.3126015663146973\n",
            "Total time for country: Fiji is 5.4941325187683105\n",
            "Total time for country: Solomon Islands is 2.9408013820648193\n",
            "Total time for country: Vanuatu is 3.2782294750213623\n",
            "Total time for country: Samoa is 2.7137160301208496\n",
            "Total time for country: Tonga is -0.0009427070617675781\n",
            "Total time for country: Kiribati is -2.0623419284820557\n",
            "Total time for country: Tuvalu is -9.399558782577515\n",
            "Total time for country: Marshall Islands is -10.583814144134521\n",
            "Total time for country: Palau is 4.033239841461182\n",
            "Total time for country: Micronesia is -0.0008416175842285156\n",
            "Total time for country: Nauru is -2.9765377044677734\n",
            "Total time for country: Cook Islands is 2.2189862728118896\n",
            "Total time for country: Niue is -0.0008838176727294922\n",
            "Total time for country: Comoros is 2.1896681785583496\n",
            "Total time for country: Maldives is 2.1256864070892334\n",
            "Total time for country: Cape Verde is 2.577415943145752\n",
            "Total time for country: SΓ£o TomΓ© and PrΓ­ncipe is 2.6751351356506348\n",
            "Total time for country: Saint Kitts and Nevis is 2.8594794273376465\n",
            "Total time for country: Saint Lucia is 2.7586190700531006\n",
            "Total time for country: Saint Vincent and the Grenadines is 1.8319523334503174\n",
            "Total time for country: Dominica is 2.2509264945983887\n",
            "Total time for country: Antigua and Barbuda is 2.793400526046753\n",
            "Total time for country: Barbados is 2.134657859802246\n",
            "Total time for country: Grenada is 1.9456777572631836\n",
            "Total time for country: Belize is 3.2146828174591064\n",
            "Total time for country: Suriname is 1.9713115692138672\n",
            "Total time for country: Guyana is 3.29117751121521\n",
            "Total time for country: Paraguay is 3.483541250228882\n"
          ]
        }
      ],
      "source": [
        "for country in most_visited_countries[:]:\n",
        "  start = time.time()\n",
        "  params = {\n",
        "    \"engine\": \"google_hotels\",\n",
        "    \"q\": f\"{country} Hotels\",\n",
        "    \"check_in_date\": \"2024-03-16\",\n",
        "    \"check_out_date\": \"2024-03-17\",\n",
        "    \"adults\": \"2\",\n",
        "    \"currency\": \"USD\",\n",
        "    \"gl\": \"us\",\n",
        "    \"hl\": \"en\",\n",
        "    \"api_key\": serpapi_key\n",
        "  }\n",
        "  try:\n",
        "    result = serpapi.search(params)\n",
        "  except:\n",
        "    result = serpapi.GoogleSearch(params)\n",
        "  try:\n",
        "    all_hotels = [] # append hotel deails\n",
        "    # change this code to deep dive into dictionaries\n",
        "    for idx in result.get_json()['properties']:\n",
        "      if idx['type'] == \"hotel\":\n",
        "        # Extract required data\n",
        "        try:\n",
        "          name = idx['name']\n",
        "          check_in_time = idx['check_in_time']\n",
        "          check_out_time = idx['check_out_time']\n",
        "          lowest_rate_before_tax = idx['rate_per_night']['before_taxes_fees']\n",
        "          lowest_rate_after_tax = idx['total_rate']['lowest']\n",
        "          extracted_hotel_class = idx['extracted_hotel_class']\n",
        "          overall_rating = idx['overall_rating']\n",
        "          nearby_places_list = []\n",
        "          nearby_places = idx['nearby_places']\n",
        "          for place in nearby_places:\n",
        "            place_name = place['name']\n",
        "            nearby_places_list.append([place_name])\n",
        "            for trans_med in place['transportations']:\n",
        "              type_ = trans_med['type']\n",
        "              duration = trans_med['duration']\n",
        "              nearby_places_list[-1].append([type_, duration])\n",
        "          ratings = []\n",
        "          for dic in idx['ratings']:\n",
        "            ratings.append(dic['count'])\n",
        "          location_rating = idx['location_rating']\n",
        "          reviews_breakdown = idx['reviews_breakdown']\n",
        "          reviews_breakdown = []\n",
        "          for dic in idx['reviews_breakdown']:\n",
        "            if dic['name'] in [\"Service\", \"Nature\", \"Property\"]:\n",
        "              positive_reviews = round((dic['positive'] / dic['total_mentioned'])*100, 0)\n",
        "              negative_reviews = round((dic['negative'] / dic['total_mentioned'])*100, 0)\n",
        "              reviews_breakdown.append((dic['name'], positive_reviews, negative_reviews))\n",
        "          reviews_str = \"\"\n",
        "          for review in reviews_breakdown:\n",
        "            reviews_str += f\"{review[0]}: Positive Reviews -> {review[1]}% Negative Reviews -> {review[2]} \\n\"\n",
        "          nearby_places_str = \"\"\n",
        "          for place in nearby_places_list:\n",
        "            # print(place)\n",
        "            for idx, temp in enumerate(place):\n",
        "              if idx == 0:\n",
        "                temp_str = temp + \"  Distance:\"\n",
        "                continue\n",
        "              temp_str += f\" {place[idx][-1]} by {place[idx][0]}\"\n",
        "            nearby_places_str += temp_str + \"\\n\"\n",
        "          hotel_details = f\"\"\"\n",
        "    Country : {country}\n",
        "    Name: {name},\n",
        "    Check in time: {check_in_time},\n",
        "    Check out time : {check_out_time},\n",
        "    Lowest rate (with tax) : {lowest_rate_after_tax},\n",
        "    Lowest rate (without tax) : {lowest_rate_before_tax},\n",
        "    Hotel Class : {extracted_hotel_class},\n",
        "    Overall rating: {overall_rating}\n",
        "    Reviews:\n",
        "      5-start: {ratings[0]}, 4-start: {ratings[1]}, 3-start: {ratings[2]}, 2-start: {ratings[3]}, 1-start: {ratings[4]},\n",
        "    Location Rating: {location_rating},\n",
        "    Reviews Breakdown:\n",
        "    {reviews_str}\n",
        "    Nearby Places:\n",
        "      {nearby_places_str}\n",
        "            \"\"\"\n",
        "          all_hotels.append(hotel_details)\n",
        "          end = time.time()\n",
        "        except Exception as e:\n",
        "          continue\n",
        "    hotels.append(all_hotels)\n",
        "    print(f\"Total time for country: {country} is {end-start}\")\n",
        "  except:\n",
        "    continue\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "hotels_str = \"\"\n",
        "for country in hotels:\n",
        "  for hotel in country:\n",
        "    hotels_str += hotel.strip() + \"\\n\\n\"\n"
      ],
      "metadata": {
        "id": "oEKIsk5qqq54"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import pickle\n",
        "with open(\"all_hotels_list.pkl\", \"wb\") as file:\n",
        "  pickle.dump(hotels, file)"
      ],
      "metadata": {
        "id": "m8Yg0Cneshej"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "with open(\"all_hotels.txt\", \"w\") as file:\n",
        "  file.write(hotels_str)"
      ],
      "metadata": {
        "id": "k_VEcbCrr2z_"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}