{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import bs4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import csv" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [], "source": [ "r = requests.get('https://www.oyorooms.com/oyos-in-kochi?adults=1&checkin=30%2F03%2F2018&checkout=06%2F04%2F2018&children=0&city=kochi&country=India&employee_id=null&guests=1&latitude=null&location=Kochi&longitude=null&page=1&rooms=1&searchType=city&src=null')" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/p17429374/Desktop/Eudora/venv/lib/python3.6/site-packages/bs4/__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system (\"html5lib\"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.\n", "\n", "The code that caused this warning is on line 193 of the file /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py. To get rid of this warning, change code that looks like this:\n", "\n", " BeautifulSoup(YOUR_MARKUP})\n", "\n", "to this:\n", "\n", " BeautifulSoup(YOUR_MARKUP, \"html5lib\")\n", "\n", " markup_type=markup_type))\n" ] } ], "source": [ "from bs4 import BeautifulSoup\n", "mypage = BeautifulSoup(r.text)" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [], "source": [ "myspan = mypage.find_all('span', attrs = {'class':'newHotelCard__hotelName'})" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OYO Flagship 240 South Railway Station East Gate" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myspan[0]" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'OYO Flagship 240 South Railway Station East Gate'" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myspan[0].text.strip()" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "hotels = []\n", "for hotel in myspan:\n", " hotels.append(hotel.text.strip())" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['OYO Flagship 240 South Railway Station East Gate',\n", " 'OYO 9902 Nedumparambil Residency',\n", " 'OYO 9954 Emarald Hotel',\n", " 'OYO 10461 Hotel White Residency',\n", " 'OYO 12329 Prime Palace Hotel',\n", " 'OYO 9560 Hotel Bellwether',\n", " 'OYO 6725 Palm Inn',\n", " 'OYO 11328 Hotel Chandrika Residency',\n", " 'OYO 4802 Cochin City residency',\n", " 'OYO 10719 Mermaid Hotel',\n", " 'OYO 11404 Hotel Tri Star Regency',\n", " 'OYO Rooms 138 South Railway Station Extention',\n", " 'OYO 7181 VR Inn',\n", " 'OYO 11909 Star Plaza',\n", " 'OYO 4824 Hotel Star',\n", " 'OYO 11309 Hotel Green Land Residency',\n", " 'OYO 10220 near Infopark',\n", " 'OYO 6665 Thomson Regency',\n", " 'OYO 10149 Noor Residency',\n", " 'OYO 8434 The Qasr']" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hotels" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [], "source": [ "mylocations = mypage.find_all('span', attrs = {'class':'newHotelCard__hotelAddress'})" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18.6 Cents , Opp. GCDA Flats, Karshaka Road, Kochi" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylocations[0]" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [], "source": [ "locations = []\n", "for location in mylocations:\n", " locations.append(location.text.strip())" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['18.6 Cents , Opp. GCDA Flats, Karshaka Road, Kochi',\n", " 'Pettah, Poonithura, Kochi',\n", " 'Major Road , Vytilla, Kochi',\n", " 'Palarivattom- Kakkanad Road, Vazhakkala, Kakkanad, Kochi',\n", " 'M.G Road Near Maharajas Metro Station, Shenoy Junction, Fashion Street Sheshadri Lane Kochi, Kochi',\n", " 'P T Jacob Rd, Opposite Kannamaly Bus Stop, Thopumpady, Kochi',\n", " 'Near Aluva Pumb Junction, Aluva, Kochi',\n", " 'Diwans Road, Near TDM Hall, Ernakulam, Kochi - 682016 Kerala, India , Diwans Road, Kochi',\n", " 'Panampilly, Kochi',\n", " 'Kaniyampuzha Road, Kochi',\n", " 'Near GCDA Head Office, SA Road, Kadavanthra, Kochi',\n", " 'Chittoor Road, Kochi',\n", " 'K R Pankajakshan Road, Kochi',\n", " 'Kariyad - Airport - Mattoor Kerala 683589, Kochi',\n", " 'Near Kaloor Metro Station, Banerji Road, Kochi',\n", " 'Monastery Road, Karikkamuri, Shenoys, Kochi',\n", " 'Karimughal Junction, Kochi',\n", " 'Near CIAL Convention Centre, Akaparambu Road, Kochi',\n", " 'Cheranaloor, Ernakulam, Kochi',\n", " 'Near Masjid Noor, Kochi']" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "locations" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [], "source": [ "myratings = mypage.find_all('span', attrs = {'class':'hotelRating__value hotelRating__value--verygood'})" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "ratings = []\n", "for rating in myratings:\n", " ratings.append(rating.text.strip())" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['4.2',\n", " '4.4',\n", " '4.2',\n", " '4.3',\n", " '4.1',\n", " '4.3',\n", " '4.3',\n", " '4.2',\n", " '4.2',\n", " '4.1',\n", " '4.3',\n", " '4.4',\n", " '4.4']" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ratings" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len (ratings)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['OYO Flagship 240 South Railway Station East Gate',\n", " '18.6 Cents , Opp. GCDA Flats, Karshaka Road, Kochi',\n", " '4.2'],\n", " ['OYO 9902 Nedumparambil Residency', 'Pettah, Poonithura, Kochi', '4.4'],\n", " ['OYO 9954 Emarald Hotel', 'Major Road , Vytilla, Kochi', '4.2'],\n", " ['OYO 10461 Hotel White Residency',\n", " 'Palarivattom- Kakkanad Road, Vazhakkala, Kakkanad, Kochi',\n", " '4.3'],\n", " ['OYO 12329 Prime Palace Hotel',\n", " 'M.G Road Near Maharajas Metro Station, Shenoy Junction, Fashion Street Sheshadri Lane Kochi, Kochi',\n", " '4.1'],\n", " ['OYO 9560 Hotel Bellwether',\n", " 'P T Jacob Rd, Opposite Kannamaly Bus Stop, Thopumpady, Kochi',\n", " '4.3'],\n", " ['OYO 6725 Palm Inn', 'Near Aluva Pumb Junction, Aluva, Kochi', '4.3'],\n", " ['OYO 11328 Hotel Chandrika Residency',\n", " 'Diwans Road, Near TDM Hall, Ernakulam, Kochi - 682016 Kerala, India , Diwans Road, Kochi',\n", " '4.2'],\n", " ['OYO 4802 Cochin City residency', 'Panampilly, Kochi', '4.2'],\n", " ['OYO 10719 Mermaid Hotel', 'Kaniyampuzha Road, Kochi', '4.1'],\n", " ['OYO 11404 Hotel Tri Star Regency',\n", " 'Near GCDA Head Office, SA Road, Kadavanthra, Kochi',\n", " '4.3'],\n", " ['OYO Rooms 138 South Railway Station Extention',\n", " 'Chittoor Road, Kochi',\n", " '4.4'],\n", " ['OYO 7181 VR Inn', 'K R Pankajakshan Road, Kochi', '4.4']]" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "content = []\n", "for i in range(0,13):\n", " content.append([hotels[i], locations[i], ratings[i]])\n", " \n", "content" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "import csv\n", "with open('Kochi_hotels.csv', 'w', newline='') as csvfile:\n", " spamwriter = csv.writer(csvfile, delimiter=',',\n", " quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n", " \n", " spamwriter.writerows(content)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }