<jupyter_start><jupyter_text>practice 1<jupyter_code>ch_driver = webdriver.Chrome('chromedriver_path')

url = 'https://www.tripadvisor.com/Restaurants-g191-United_States.html'
ch_driver.get(url)

ch_driver.find_element_by_class_name('continue').click()

cities = ch_driver.find_elements_by_class_name('geo_name')

# for check
len(cities)
cities[0].find_element_by_tag_name('a').click()
sleep(3)
soup = bs(ch_driver.page_source, "html.parser")
results = soup.find('div', attrs={'id':'EATERY_SEARCH_RESULTS'}) 
restaurant = results.find('div', attrs={'data-index': '1'})
# get restaurant name
title_div = restaurant.find('div', attrs={'class': 'title'})
title = title_div.find('a').text.strip('\n')
title
# get rating
rating = restaurant.find('span', attrs={'class' : 'ui_bubble_rating bubble_45'}).attrs['alt']
rating
# get review count
count = restaurant.find('span', attrs={'class' : 'reviewCount'}).find('a').text.strip('\n')
count
# get price
try:
    price = restaurant.find('span', attrs={'class' : 'item price'}).text
except:
    price = "N/A"
price
# get cuisine
try:
    cuisine_div = restaurant.find('div', attrs={'class' : 'cuisines'}).find_all('a')
    cuisine = []
    for div in cuisine_div:
        cuisine.append(div.text)
except:
    cuisine = []
cuisine
review_ul = restaurant.find('ul', attrs={'class' : 'review_stubs review_snippets rebrand'})
review_a = review_ul.find_all('a')
review = []
for tag in review_a:
    review.append(tag.text)
    
review<jupyter_output><empty_output><jupyter_text>practice 2<jupyter_code>ch_driver.back()
cities_div = ch_driver.find_elements_by_class_name('geo_name')
city_list = []
for item in cities_div:
    name = item.find_element_by_tag_name('a').text
    city_list.append(name)
city_list
#city name slicing
for city in city_list:
    print(city[:-12])
cities_div[0].find_element_by_tag_name('a').click()
soup = bs(ch_driver.page_source, "html.parser")
results = soup.find('div', attrs={'id':'EATERY_SEARCH_RESULTS'}) 
rank = 1
restaurant = results.find('div', attrs={'data-index': '{rank}'.format(rank = rank)})
cuisine_div = restaurant.find('div', attrs={'class' : 'cuisines'}).find_all('a')
cuisine_list = []
for div in cuisine_div:
    cuisine_list.append(div.text)
cuisine = ", ".join(cuisine_list)

cuisine
review_ul = restaurant.find('ul', attrs={'class' : 'review_stubs review_snippets rebrand'})
review_a = review_ul.find_all('a')
review_list = []
for tag in review_a:
    review_list.append(tag.text)
review = ", ".join(review_list)

review
def scraper():
    result_list = []
    for i in range(1, 30+1):
        soup = bs(ch_driver.page_source, "html.parser")
        results = soup.find('div', attrs={'id':'EATERY_SEARCH_RESULTS'}) 
        rank = str(i)
        restaurant = results.find('div', attrs={'data-index': '{rank}'.format(rank = rank)})

        title_div = restaurant.find('div', attrs={'class' : 'title'})
        title = title_div.find('a').text.strip('\n')

        rating_div = restaurant.find('div', attrs={'class' : 'rating rebrand'})
        rating = rating_div.find('span').attrs['alt']

        count = restaurant.find('span', attrs={'class' : 'reviewCount'}).find('a').text.strip('\n')

        try:
            price = restaurant.find('span', attrs={'class' : 'item price'}).text
            cuisine_div = restaurant.find('div', attrs={'class' : 'cuisines'}).find_all('a')
            cuisine_list = []
            for div in cuisine_div:
                cuisine_list.append(div.text)
            cuisine = ", ".join(cuisine_list)
        except:
            price = "N/A"
            cuisine = "N/A"

        review_ul = restaurant.find('ul', attrs={'class' : 'review_stubs review_snippets rebrand'})
        review_a = review_ul.find_all('a')
        review_list = []
        for tag in review_a:
            review_list.append(tag.text)
        review = ", ".join(review_list)

        result_list.append((title, rating, count, price, cuisine, review))
        
    return result_list
scraper()[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16907011437095973, 'fraction_numerical': 0.004724017901541522, 'mean_word_length': 4.190967741935484, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '504806', 'license': 'MIT', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1414, 'n_words': 243, 'path': 'TripAdvisor_WebScraping_Practice.ipynb', 'repo_name': 'mindudekim/Restaurant-research', 'size': '4022'}