Spaces:
Sleeping
Sleeping
def Talabat_location_links(location): | |
import requests | |
from bs4 import BeautifulSoup | |
import re | |
from Talabat_files.main_function import main_all_extract | |
# Function to convert text to kebab case | |
def to_kebab_case(text: str) -> str: | |
text = text.lower() # Convert to lowercase | |
text = re.sub(r'\s+', '-', text) # Replace spaces with dashes | |
text = re.sub(r'[^a-zA-Z0-9-]', '', text) # Remove non-alphanumeric characters | |
return text | |
# Function to fetch location code | |
def location_code(name: str) -> int: | |
url = "https://www.talabat.com/nextApi/location/country-areas/4" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36', | |
'Accept-Language': 'en-US', | |
'Cookie': '__cf_bm=_mZexeD7G1shqe.pO2VMyyTt.NCBm6Tt1saj4P.MQDk-1716294515-1.0.1.1-3nmOwncAyqyw5pak8.DFvLZjZYKQv4yW16.7jRG6groz0869YtR83cK8uy_VR70ebNG7mLMgRPRHxhTndBWJUGFQUlk; AWSALB=mS5qvg1IVA+fbSMlvbIt2qv6TrpVbPLMF2YveRn8OJQCwHyPfnEVsOhdmOOFINqeB93N6kx9xPbjNggoc9pBVTfQWCVeVayfSPMeOBQKSXFZ/ppIMGjDMTX3o+BP; AWSALBCORS=mS5qvg1IVA+fbSMlvbIt2qv6TrpVbPLMF2YveRn8OJQCwHyPfnEVsOhdmOOFINqeB93N6kx9xPbjNggoc9pBVTfQWCVeVayfSPMeOBQKSXFZ/ppIMGjDMTX3o+BP; dhhPerseusGuestId=1716186301782.797703851415064000.04xvpecztvkg; next-i18next=en; tlb_country=uae; tlb_lng=en' | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() # Raise an error for bad status codes | |
jsondata = response.json() | |
for data in jsondata.get('areas', []): | |
if name.lower() == data['cityName'].lower() or name.lower() == data['name'].lower(): | |
return data['id'] | |
return None | |
except requests.RequestException as e: | |
print(f"An error occurred: {e}") | |
return None | |
location_name_input = location.strip() | |
code = location_code(location_name_input) | |
if code is not None: | |
location_name = to_kebab_case(location_name_input) | |
url = f"https://www.talabat.com/uae/restaurants/{code}/{location_name}" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36', | |
'Cookie': '__cf_bm=pUdFBMFwQSdL5rRym8o1cr4lAKqQLqpz661jnZp9q_M-1716188106-1.0.1.1-P989rK8kUjlPuclwf7GVdcxuX9SXwyF8r4eelpQtsA7M1yCX82n4b74tSDaNTPdILiY7SHC8qXP61WKIFX1RcsV4cdtVsKmEq9Adzzo1Mxs; AWSALB=HWer/ht79Hy5nHr3VOFEqnaCc0UyDHrkYx/xyAYcq3bIe8SybCBoqXaT9B4yG/08Xhy2KaCjgh75x3No44IhEJbg8cy/n+opvCEW2lfo4TOW2MBorkBSyQ1GQ6HY; AWSALBCORS=HWer/ht79Hy5nHr3VOFEqnaCc0UyDHrkYx/xyAYcq3bIe8SybCBoqXaT9B4yG/08Xhy2KaCjgh75x3No44IhEJbg8cy/n+opvCEW2lfo4TOW2MBorkBSyQ1GQ6HY; dhhPerseusGuestId=1716186301782.797703851415064000.04xvpecztvkg; next-i18next=en; tlb_country=uae; tlb_lng=en' | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
response.raise_for_status() # Raise an error for bad status codes | |
soup = BeautifulSoup(response.text, 'html.parser') | |
restaurant_list_div = soup.find('div', class_='restaurant-list') | |
if restaurant_list_div: | |
links = restaurant_list_div.find_all('a', {'data-testid': 'restaurant-a'}) | |
hrefs = ['https://www.talabat.com' + link.get('href') for link in links] | |
print(f"Found {len(hrefs)} Links") | |
for href in hrefs: | |
print(href) | |
main_all_extract(href) | |
else: | |
print("No restaurant list found.") | |
except requests.RequestException as e: | |
print(f"An error occurred while fetching the restaurant data: {e}") | |
else: | |
print(f"Location '{location_name_input}' not found.") | |