I am using this URL https://www.tripadvisor.in/Restaurant_Review-g32655-d7222445-Reviews-The_Anchor-Los_Angeles_California.html, where there is a svg
view box when I try to click it shows working hours. I need to extract those using python selenium. Could anyone please help? I am new to web scraping.
Asked
Active
Viewed 40 times
0

Ajeet Verma
- 2,938
- 3
- 13
- 24

hari ram
- 35
- 4
2 Answers
1
The data about opening hours is stored inside the HTML page in Json form, so to get the opening hours, you can use this example:
import re
import json
import requests
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
}
url = "https://www.tripadvisor.in/Restaurant_Review-g32655-d7222445-Reviews-The_Anchor-Los_Angeles_California.html"
html_text = requests.get(url, headers=headers).text
data = re.search(r"window\.__WEB_CONTEXT__=(\{.*\});\(", html_text).group(1)
data = data.replace("pageManifest", '"pageManifest"')
data = json.loads(data)
data = data["pageManifest"]["redux"]["api"]["responses"]
for k, v in data.items():
if "/hours" in k:
print(v)
break
Prints:
{
"data": {
"openStatus": "CLOSED",
"openStatusText": "Closed Now",
"hoursTodayText": "Hours Today: 4:00 pm - 11:59 pm",
"currentHoursText": "",
"allOpenHours": [
{"days": "Tue - Fri", "times": ["4:00 pm - 11:59 pm"]},
{"days": "Sat - Sun", "times": ["11:00 am - 11:59 pm"]},
],
"addHoursLink": {
"url": "/UpdateListing-d7222445#Hours-only",
"text": "+ Add hours",
},
},
"error": None,
}

Andrej Kesely
- 168,389
- 15
- 48
- 91
-
1Thanks it is working fine and changes it to work in a selenium driver @Andrej Kesely – hari ram Jul 27 '23 at 20:19
0
To click on the SVG element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
Code block:
driver.get("https://www.tripadvisor.in/Restaurant_Review-g32655-d7222445-Reviews-The_Anchor-Los_Angeles_California.html") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[data-automation='top-info-hours'] > div svg[width='18px'] path:nth-child(2)"))).click() print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Hours']//following::div[1]"))).text)
Console output:
Sun 11:00 AM - 11:59 PM Tue 4:00 PM - 11:59 PM Wed 4:00 PM - 11:59 PM Thu 4:00 PM - 11:59 PM Fri 4:00 PM - 11:59 PM Sat 11:00 AM - 11:59 PM
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

undetected Selenium
- 183,867
- 41
- 278
- 352
-
1
-
@hariram Glad to be able to help you. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – undetected Selenium Jul 28 '23 at 09:38