-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
145 lines (120 loc) · 5.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
from bs4 import BeautifulSoup
import json
import os
import signal
import argparse
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
countries = ["au", "be", "ca", "cl", "cr", "do", "ec", "sv", "fr", "de", "gt", "ie", "jp", "ke", "mx", "nl", "nz", "pa", "pl", "pt", "za", "es", "lk", "se", "ch", "tw", "gb"]
parser = argparse.ArgumentParser(description="Scrape Uber Eats data")
parser.add_argument("--country", "-c", type=str, nargs='+', help="Scrape data from a specific country. \nIf not specified, all countries will be scraped.", metavar="<COUNTRYCODE>")
args = parser.parse_args()
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def end(signal, frame):
print("Exiting...")
with open(f"countries/{c}.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)
exit(0)
# Register the signal handler for Ctrl+C
signal.signal(signal.SIGINT, end)
# the actual stuff
if args.c == None:
clear()
print("Scraping all countries...")
for c in countries:
country = requests.get(f"https://restcountries.com/v3.1/alpha/{c}?fields=name", headers=headers, timeout=10).json()["name"]["common"]
# Check if the 'countries' folder exists, create it if it doesn't
if not os.path.exists('countries'):
os.makedirs('countries')
data = {
"country": country.upper(),
"cities": []
}
print(f"Scraping {country}...")
url = f"https://www.ubereats.com/{c}/location"
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print("An error occurred:", e)
exit(1)
soup = BeautifulSoup(response.content, "html.parser")
links = soup.find_all('a')
for link in links:
href = link.get('href') # Get href attribute if it exists
name = link.get_text().strip()
if href and href.startswith(f"/{c}/city"):
city_url = f"https://www.ubereats.com{href}"
city_data = {
"city": name,
"shops": []
}
city_response = requests.get(city_url, headers=headers, timeout=10)
city_soup = BeautifulSoup(city_response.content, "html.parser")
shops = city_soup.find_all('a', {"data-test": "store-link"})
for shop in shops:
path = shop.get('href')
page_link = "https://www.ubereats.com" + path
names = shop.find_all('h3')
for name in names:
restaurant_name = name.get_text().strip()
shop_data = {
"name": restaurant_name,
"link": page_link
}
city_data["shops"].append(shop_data)
data["cities"].append(city_data)
end()
else:
for c in args.c:
if c not in countries:
print(f"Invalid country code: {c}")
exit(1)
for c in args.c:
clear()
country = requests.get(f"https://restcountries.com/v3.1/alpha/{c}?fields=name", headers=headers, timeout=10).json()["name"]["common"]
# Check if the 'countries' folder exists, create it if it doesn't
if not os.path.exists('countries'):
os.makedirs('countries')
data = {
"country": country.upper(),
"cities": []
}
print(f"Scraping {country}...")
url = f"https://www.ubereats.com/{c}/location"
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print("An error occurred:", e)
exit(1)
soup = BeautifulSoup(response.content, "html.parser")
links = soup.find_all('a')
for link in links:
href = link.get('href') # Get href attribute if it exists
name = link.get_text().strip()
if href and href.startswith(f"/{c}/city"):
city_url = f"https://www.ubereats.com{href}"
city_data = {
"city": name,
"shops": []
}
city_response = requests.get(city_url, headers=headers, timeout=10)
city_soup = BeautifulSoup(city_response.content, "html.parser")
shops = city_soup.find_all('a', {"data-test": "store-link"})
for shop in shops:
path = shop.get('href')
page_link = "https://www.ubereats.com" + path
names = shop.find_all('h3')
for name in names:
restaurant_name = name.get_text().strip()
shop_data = {
"name": restaurant_name,
"link": page_link
}
city_data["shops"].append(shop_data)
data["cities"].append(city_data)
end()