Skip to content

Commit 9746e1e

Browse files
committed
wrap the name extraction code in function
1 parent b50b501 commit 9746e1e

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

restaurant_names.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,32 @@
33

44
base_url = "https://www.yelp.com/search?find_desc=Restaurants&find_loc={}&start={}"
55
city = "los angeles"
6+
file_path = 'los_angeles_restaurants.txt'
67

8+
def extract_names(city, file_path):
9+
with open(file_path, 'a') as file:
10+
start = 0
11+
for i in range(100):
12+
url = base_url.format(city, start)
13+
response = requests.get(url)
14+
start += 30
15+
print(f"STATUS CODE: {response.status_code} FOR {response.url}")
16+
soup = BeautifulSoup(response.text, 'html.parser')
17+
names = soup.findAll('a', {'class': 'biz-name'})
718

8-
with open('los_angeles_restaurants.txt', 'a') as file:
9-
start = 0
10-
for i in range(100):
11-
url = base_url.format(city, start)
12-
response = requests.get(url)
13-
start += 30
14-
print(f"STATUS CODE: {response.status_code} FOR {response.url}")
15-
soup = BeautifulSoup(response.text, 'html.parser')
16-
names = soup.findAll('a', {'class': 'biz-name'})
19+
count = 0
20+
for info in names:
21+
try:
22+
title = info.text
23+
print(title)
24+
file.write(title + '\n')
25+
count += 1
26+
except Exception as e:
27+
print(e)
28+
print(f"{count} RESTAURANTS EXTRACTED...")
29+
print(start)
30+
if start == 990:
31+
break
1732

18-
count = 0
19-
for info in names:
20-
try:
21-
title = info.text
22-
print(title)
23-
file.write(title + '\n')
24-
count += 1
25-
except Exception as e:
26-
print(e)
27-
print(f"{count} RESTAURANTS EXTRACTED...")
28-
print(start)
29-
if start == 30:
30-
break
33+
34+
extract_names(city, file_path)

0 commit comments

Comments
 (0)