-
Notifications
You must be signed in to change notification settings - Fork 8
/
yellow_pages_api.py
224 lines (187 loc) · 10.6 KB
/
yellow_pages_api.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import requests
from datetime import datetime
from bs4 import BeautifulSoup
class YpApi:
def __init__(self):
self.html_file = None
self.search_terms = None
self.extractor_path = None
self.geo_location_terms = None
self.rank_plier_path = "&page="
self.extractor_path_multi = None
self.route_path = "https://www.yellowpages.com/"
self.now = str(datetime.now()).replace(" ", "--").split(".")[0].replace(":", "-")
def __repr__(self):
return f"Scraper object Initialized, with a route at {self.route_path} at {self.now.split('--')[1]}"
# To generate the link for scraping
def gen_extractor_link(self, search_term, geo_location_term, plier_rank=1):
end_point = f"search?search_terms={search_term}&geo_location_terms={geo_location_term}"
self.extractor_path = self.route_path + end_point + self.rank_plier_path + f"{plier_rank}"
return self.extractor_path
# Param parser for checking None objects
def param_parser(self, param, href_parse=0):
if href_parse:
parse_element = param.a["href"] if param.a != None else None
else:
parse_element = param.text if param != None else None
return parse_element
# To generate the file name
def name_gen(self, search_term, geo_location_term):
self.now += f"_{search_term}_{geo_location_term}"
return self.now
# To scrape the webiste
def scrape_extractor(self, search_term, geo_location_term, plier_rank=1, print_data=False):
self.search_terms = search_term
self.geo_location_terms = geo_location_term
self.extractor_path = self.gen_extractor_link(self.search_terms, self.geo_location_terms, plier_rank)
self.html_file = requests.get(self.extractor_path).text
soup = BeautifulSoup(self.html_file, "lxml")
categories = soup.find_all("div", class_="result")
api_arr = []
if print_data:
print("")
for category in categories:
name = category.find("a", class_="business-name")
status = self.param_parser(category.find("div", class_="open-status"))
if (name != None):
types = category.find("div", class_="categories").find_all("a")
num = self.param_parser(category.find("h2", class_="n"))
ph_num = self.param_parser(category.find("div", class_="phone"))
info_website = name["href"]
website = self.param_parser(category.find("div", class_="links"), href_parse=1)
address = self.param_parser(category.find("div", class_="adr"))
if print_data:
print(f"{num}")
print(f"Name: {name.text}")
print(f"Phone number: {ph_num}")
print(f"Categories: {[type_.text for type_ in types]}")
print(f"Status: {status}")
print(f"Address: {address}")
print(f"Website: {website}")
print(f"More info: https://www.yellowpages.com{info_website}")
print("-------------------------------------------------")
print("")
dict_ = {"Name": name.text,
"Phone-number": ph_num,
"Categories": [type_.text for type_ in types],
"Status": status,
"Address": address,
"Website": website,
"More-info": f"https://www.yellowpages.com{info_website}"}
api_arr.append(dict_)
return api_arr
# To write the scraped details in a file
def scrape_extractor_in_file(self, search_term, geo_location_term, plier_rank=1):
self.search_terms = search_term
self.geo_location_terms = geo_location_term
self.now = self.name_gen(self.search_terms, self.geo_location_terms)
self.extractor_path = self.gen_extractor_link(self.search_terms, self.geo_location_terms, plier_rank)
self.html_file = requests.get(self.extractor_path).text
soup = BeautifulSoup(self.html_file, "lxml")
categories = soup.find_all("div", class_="result")
with open(f"Yellow-pages/{self.now}.txt", "w") as f:
for category in categories:
name = category.find("a", class_="business-name")
status = self.param_parser(category.find("div", class_="open-status"))
if (name != None):
types = category.find("div", class_="categories").find_all("a")
num = self.param_parser(category.find("h2", class_="n"))
ph_num = self.param_parser(category.find("div", class_="phone"))
info_website = name["href"]
website = self.param_parser(category.find("div", class_="links"), href_parse=1)
address = self.param_parser(category.find("div", class_="adr"))
f.write(f"{num}\n")
f.write(f"Name: {name.text}\n")
f.write(f"Phone number: {ph_num}\n")
f.write(f"Categories: {[type_.text for type_ in types]}\n")
f.write(f"Status: {status}\n")
f.write(f"Address: {address}\n")
f.write(f"Website: {website}\n")
f.write(f"More info: https://www.yellowpages.com{info_website}\n")
f.write("-------------------------------------------------\n")
f.write("\n")
# To scrape multiple pages and generate more details for api
def scrape_extractor_multi(self, search_term, geo_location_term, plier=10, print_data=False):
self.search_terms = search_term
self.geo_location_terms = geo_location_term
api_arr = []
for plier_rank in range(1, plier+1):
self.extractor_path_multi = self.gen_extractor_link(self.search_terms,
self.geo_location_terms,
plier_rank)
self.html_file = requests.get(self.extractor_path_multi).text
soup = BeautifulSoup(self.html_file, "lxml")
categories = soup.find_all("div", class_="result")
if print_data:
print("")
for category in categories:
name = category.find("a", class_="business-name")
status = self.param_parser(category.find("div", class_="open-status"))
if (name != None):
types = category.find("div", class_="categories").find_all("a")
num = self.param_parser(category.find("h2", class_="n"))
ph_num = self.param_parser(category.find("div", class_="phone"))
info_website = name["href"]
website = self.param_parser(category.find("div", class_="links"), href_parse=1)
address = self.param_parser(category.find("div", class_="adr"))
if print_data:
print(f"{num}")
print(f"Name: {name.text}")
print(f"Phone number: {ph_num}")
print(f"Categories: {[type_.text for type_ in types]}")
print(f"Status: {status}")
print(f"Address: {address}")
print(f"Website: {website}")
print(f"More info: https://www.yellowpages.com{info_website}")
print("-------------------------------------------------")
print("")
dict_ = {"Name": name.text,
"Phone-number": ph_num,
"Categories": [type_.text for type_ in types],
"Status": status,
"Address": address,
"Website": website,
"More-info": f"https://www.yellowpages.com{info_website}"}
api_arr.append(dict_)
return api_arr
# To write the details of multiple pages in a single file
def scrape_extractor_multi_in_file(self, search_term, geo_location_term, plier=10):
self.search_terms = search_term
self.geo_location_terms = geo_location_term
self.now = self.name_gen(self.search_terms, self.geo_location_terms) + f"_{plier}"
with open(f"Yellow-pages/{self.now}.txt", "w") as f:
for plier_rank in range(1, plier + 1):
self.extractor_path_multi = self.gen_extractor_link(self.search_terms,
self.geo_location_terms,
plier_rank)
self.html_file = requests.get(self.extractor_path_multi).text
soup = BeautifulSoup(self.html_file, "lxml")
categories = soup.find_all("div", class_="result")
for category in categories:
name = category.find("a", class_="business-name")
status = self.param_parser(category.find("div", class_="open-status"))
if (name != None):
types = category.find("div", class_="categories").find_all("a")
num = self.param_parser(category.find("h2", class_="n"))
ph_num = self.param_parser(category.find("div", class_="phone"))
info_website = name["href"]
website = self.param_parser(category.find("div", class_="links"), href_parse=1)
address = self.param_parser(category.find("div", class_="adr"))
f.write(f"{num}\n")
f.write(f"Name: {name.text}\n")
f.write(f"Phone number: {ph_num}\n")
f.write(f"Categories: {[type_.text for type_ in types]}\n")
f.write(f"Status: {status}\n")
f.write(f"Address: {address}\n")
f.write(f"Website: {website}\n")
f.write(f"More info: https://www.yellowpages.com{info_website}\n")
f.write("-------------------------------------------------\n")
f.write("\n")
# To return tha array of api into a dictionary
def get_api(self, json_arr):
res_dict = {int(i + 1): elem for i, elem in enumerate(json_arr)}
return res_dict
if __name__ == "__main__":
api = YpApi()
api.scrape_extractor_multi_in_file("auto-repair", "detroit", plier=2)
# print(api)