-
Notifications
You must be signed in to change notification settings - Fork 2
/
writing_details.py
43 lines (37 loc) · 1.09 KB
/
writing_details.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
import requests
from bs4 import BeautifulSoup
base_url = "https://www.yelp.com/search?find_desc=Restaurants&find_loc={}&start={}"
city = "los+angeles"
start = 0
file_path = f'yelp-{city}.txt'
while start < 990:
print(start)
url = base_url.format(city, start)
response = requests.get(url)
print(f"STATUS CODE: {response.status_code} FOR {response.url}")
soup = BeautifulSoup(response.text, 'html.parser')
businesses = soup.findAll('div', {'class': 'biz-listing-large'})
with open(file_path, 'a') as textFile:
count = 0
for biz in businesses:
try:
title = biz.find('a', {'class': 'biz-name'}).text
address = biz.find('address').text
phone = biz.find('span', {'class': 'biz-phone'}).text
count += 1
except Exception as e:
print(e)
logs = open('errors.log', 'a')
logs.write(str(e) + '\n')
logs.close()
address = None
phone = None
detail = f"{title}\n{address}\n{phone}"
print(detail)
try:
textFile.write(str(detail) + '\n\n')
except Exception as e:
logs = open('errors.log', 'a')
logs.write(str(e) + '\n')
logs.close()
start += 30