|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | + |
| 4 | +base_url = "https://www.yelp.com/search?find_desc=Restaurants&find_loc={}&start={}" |
| 5 | +city = "los+angeles" |
| 6 | +start = 0 |
| 7 | +file_path = f'yelp-{city}-clean.txt' |
| 8 | + |
| 9 | +while start < 60: |
| 10 | + print(start) |
| 11 | + url = base_url.format(city, start) |
| 12 | + response = requests.get(url) |
| 13 | + print(f"STATUS CODE: {response.status_code} FOR {response.url}") |
| 14 | + soup = BeautifulSoup(response.text, 'html.parser') |
| 15 | + businesses = soup.findAll('div', {'class': 'biz-listing-large'}) |
| 16 | + |
| 17 | + with open(file_path, 'a') as textFile: |
| 18 | + count = 0 |
| 19 | + for biz in businesses: |
| 20 | + try: |
| 21 | + title = biz.find('a', {'class': 'biz-name'}).text |
| 22 | + address = biz.find('address').contents |
| 23 | + print(address) |
| 24 | + phone = biz.find('span', {'class': 'biz-phone'}).text |
| 25 | + count += 1 |
| 26 | + except Exception as e: |
| 27 | + print(e) |
| 28 | + logs = open('errors.log', 'a') |
| 29 | + logs.write(str(e) + '\n') |
| 30 | + logs.close() |
| 31 | + address = None |
| 32 | + phone = None |
| 33 | + |
| 34 | + detail = f"{title}\n{address}\n{phone}" |
| 35 | + print(detail) |
| 36 | + |
| 37 | + try: |
| 38 | + textFile.write(str(detail) + '\n\n') |
| 39 | + except Exception as e: |
| 40 | + logs = open('errors.log', 'a') |
| 41 | + logs.write(str(e) + '\n') |
| 42 | + logs.close() |
| 43 | + |
| 44 | + start += 30 |
| 45 | + |
0 commit comments