-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
135 lines (123 loc) · 4.55 KB
/
scraper.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
from bs4 import BeautifulSoup
from time import sleep
import requests
import json
import re
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
user_agent = (
'Mozilla/5.0 (X11; Linux x86_64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/106.0.0.0 Safari/537.36'
)
default_headers = {'User-Agent':user_agent}
def get_max_page() -> int:
""" Maximum number of pages """
session = requests.Session()
response = session.get('https://pergikuliner.com/restaurants')
page = BeautifulSoup(response.text, 'html.parser')
text = page.find('h2', {'id':'top-total-search-view'})
text = text.find('strong').text.split('dari')
page_num = [int(t.strip()) for t in text]
return int(page_num[1]/page_num[0])
def scrape_page(response):
page = BeautifulSoup(response.text, 'html.parser')
cards = page.find_all('div', class_='restaurant-result-wrapper')
data = []
for item in cards:
# title and link
title = item.find('h3').text.strip()
link = 'https://pergikuliner.com' + item.find('a')['href']
# food style and location
description = item.find('div', class_='item-group').find('div').text.strip()
description = description.split('|')
if len(description) > 1:
location = description[0].strip()
cuisine = [i.strip() for i in description[1].split(',')]
else:
location = description
cuisine = None
# rating
full_rate = item.find('div', class_='item-rating-result').find('small').text.strip()
rate = item.find('div', class_='item-rating-result').text.replace(full_rate,'').strip()
full_rate = full_rate.replace('/','')
# location and price
for p in item.find_all('p', class_='clearfix'):
if 'icon-map' in p.find('i')['class']:
place = p.find_all('span', class_='truncate')
address = place[0].text.strip()
street = place[1].text.strip()
elif 'icon-price' in p.find('i')['class']:
price_text = p.find('span').text.strip()
if re.findall(r'-', price_text):
price_from = price_text.split('-')[0].strip()
price_till = price_text.split('-')[1].replace('/orang','').strip()
elif re.findall(r'Di atas', price_text):
price_from = price_text.replace('Di atas','').replace('/orang','').strip()
price_till = None
elif re.findall(r'Di bawah', price_text):
price_from = 'Rp. 0'
price_till = price_text.replace('Di bawah','').replace('/orang','').strip()
else:
logging.info(f"Another condition in price")
price_from = price_text
price_till = price_text
else:
logging.info(f"Something else in location and price section")
item_data = {
'title': title,
'rate': rate,
'cuisine': cuisine,
'location': location,
'address': address,
'street': street,
'price_from': price_from,
'price_till': price_till,
'url': link,
}
data.append(item_data)
return data
def crawl(npage=None):
""" Crawl pages """
session = requests.Session()
session.headers.update(default_headers)
if npage is None:
npage = get_max_page() + 1
data = []
for n in range(1, npage):
params = {'page': n}
""" params = {
'advance': '1',
'average_price': '',
'average_rate': '',
'default_search': '',
'latitude': '',
'longitude': '',
'meals': '',
'open_at': '',
'page': '2',
'rate_rasa': 'true',
'search_name_cuisine': '',
'search_place': '',
'sort_by': 'pergikuliner',
}
"""
try:
response = session.get('https://pergikuliner.com/restaurants', params=params)
logging.info(f"({response.status_code}) GET page {n}")
data += scrape_page(response)
sleep(1)
except Exception as e:
logging.error(f"Error in {n}: {e}")
pass
return data
def save_data(data, filename):
with open(filename, 'w') as f:
json.dump(data, f)
if __name__ == '__main__':
data = crawl()
save_data(data, "data.json")