Im new to coding! I found a large part of this script in a github that seems inactive now. Basically this script is scraping reviews of only one Tripadvisor restaurant. Depending on number of pages (same restaurant) it has to scrape the script either finishes normally or gives the following error when there is no more next button:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .//a[@class="nav next ui_button primary"]
I would like the script to continue on to another URL in case it is finished with first restaurant or gets error for no next button. Help me guys!
import sys
import csv
from selenium import webdriver
import time
path_to_file = "entirereview.csv"
num_page = 12
url = "https://www.tripadvisor.com/Restaurant_Review-g187147-d23117361-Reviews-Ya_Bayte-Paris_Ile_de_France.html"
if (len(sys.argv) == 4):
path_to_file = sys.argv[1]
num_page = int(sys.argv[2])
url = sys.argv[3]
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_id('_evidon-accept-button').click()
driver.find_element_by_id('filters_detail_language_filterLang_ALL').click()
csvFile = open(path_to_file, 'a', encoding="utf-8")
csvWriter = csv.writer(csvFile)
for i in range(0, num_page):
time.sleep(3)
driver.find_element_by_xpath("//span[@class='taLnk ulBlueLinks']").click()
time.sleep(3)
container = driver.find_elements_by_xpath(".//div[@class='review-container']")
for j in range(len(container)):
title = container[j].find_element_by_xpath(".//span[@class='noQuotes']").text
date = container[j].find_element_by_xpath(".//span[contains(@class, 'ratingDate')]").get_attribute("title")
rating = container[j].find_element_by_xpath(".//span[contains(@class, 'ui_bubble_rating bubble_')]").get_attribute("class").split("_")[3]
review = container[j].find_element_by_xpath(".//p[@class='partial_entry']").text.replace("\n", " ")
csvWriter.writerow([date, rating, title, review])
driver.find_element_by_xpath('.//a[@class="nav next ui_button primary"]').click()
driver.close()
try/exceptto catch it.