Sunday, November 14, 2021

selenium 11 booking bot report


sort by price

select property box

find price


pretty table report
#logs

# of hotels 25
[['Jazz on the Park Youth Hostel', 'US$234', '5.9'], ['HI NYC Hostel', 'US$252', '8.4'], ['Jazz on Columbus Circle Hostel', 'US$297', '7
.7'], ['NYC Manhattan Bedroom', 'US$470', '6.9'], ['West Side YMCA', 'US$485', '7.3'], ['Hotel Henny', 'US$525', '4.8'], ['Pod 51', 'US$
616', '8.0'], ['Night Hotel Broadway', 'US$681', '6.2'], ['Holiday Inn Express - Wall Street, an IHG Hotel', 'US$690', '7.5'], ['Pod 39'
, 'US$690', '8.3'], ['The Harlem Getaway', 'US$705', '7.4'], ['Chelsea Inn', 'US$722', '8.2'], ['Solita Soho Hotel', 'US$724', '6.8'], [
'The International Cozy Inn', 'US$725', '8.0'], ['Pod Times Square', 'US$727', '8.4'], ['Colonial House Inn', 'US$738', '8.5'], ['Orchar
d Street Hotel', 'US$758', '7.6'], ['Courtyard New York Downtown Manhattan/Financial District', 'US$769', '7.1'], ['The Gatsby Hotel', '
US$786', '7.2'], ['Hotel Newton', 'US$786', '8.2'], ['Hotel 32 32', 'US$792', '7.6'], ['Hotel Hayden New York', 'US$794', '7.9'], ['Seto
n Hotel', 'US$797', '8.5'], ['U Hotel Fifth Avenue', 'US$804', '8.1'], ['Holiday Inn Manhattan Financial District, an IHG Hotel', 'US$80
8', '8.1']]

------------------------------
#Booking/booking.py

from time import sleep
import Booking.constants as constants
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Booking.filters import BookingFilters
from Booking.report import Report
from prettytable import PrettyTable

class Booking(webdriver.Chrome):
    def __init__(self, path=r"C:/Users/zchen/PycharmProjects/selenium", auto_close=True):
        self.driver_path = path
        self.auto_close = auto_close
        os.environ['PATH'] += self.driver_path
        super(Booking, self).__init__()

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.auto_close:
            print('exiting...')
            self.quit()

    def open_page(self):
        self.get(constants.BASE_URL)
        print(constants.BASE_URL, 'is opened')

    ...

    def report(self):
        report_class = Report(driver=self)

        properties = self.find_elements(
            By.CSS_SELECTOR,
            'div[data-testid="property-card"]'
        )
        print("# of hotels", len(properties))

        hotel_list = report_class.report(hotels=properties)
        print(hotel_list)
        table = PrettyTable(
            field_names=["Name", "Price", "Score"]
        )
        table.add_rows(hotel_list)
        print(table)

---------------------
#Booking/report.py

from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.by import By

class Report:
    def __init__(self, driver:WebDriver):
        self.driver = driver

    def report(self, hotels):
        hotel_list = []
        for hotel in hotels:
            name = hotel.find_element(
                By.CSS_SELECTOR,
                'div[data-testid="title"]'
            ).get_attribute('innerHTML')

            price = hotel.find_element(
                By.CSS_SELECTOR,
                'div[data-testid="price-and-discounted-price"]'
            ).find_elements(
                By.TAG_NAME,
                'span'
            )[-1].get_attribute('innerHTML')

            score = hotel.find_element(
                By.CSS_SELECTOR,
                'div[aria-label^="Scored"]'
            ).get_attribute('innerHTML')

            hotel_list.append([name, price, score])

        return hotel_list

-------------------------
#run.py

from Booking.booking import Booking

with Booking(auto_close=False) as bot:
    bot.open_page()
    bot.change_currency(currency='USD')
    bot.select_place_to_go("New York")
    bot.select_dates('2021-11-20', '2021-11-25')
    bot.update_adult_room_number(1, 3)
    bot.search()
    bot.filters(3, 4, 5)
    bot.refresh()
    bot.report()

reference:

No comments:

Post a Comment