-
Notifications
You must be signed in to change notification settings - Fork 0
/
pakwheel_per_page.py
346 lines (309 loc) · 10.7 KB
/
pakwheel_per_page.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
Author: MrAsimZahid
website: Mrasimzahid.github.io
Project: PakWheels automobiles(specificlly cars) data
Start date: October 18, 2021
End Date: October 23, 2021
"""
"""
Structure:
1-Scrap per page(used cars)
2-Pick JSON available
3-<div class="row ad-listing-template mt10">
<div class="col-md-8">
<script type="application/ld+json">
4-Few features
-last updated(important feature)
<ul class="list-unstyled ul-featured clearfix" id="scroll_car_detail">
5-Car features
<ul class="list-unstyled car-feature-list nomargin">
6-Pick location(done)
<p class="detail-sub-heading">
<a href="#"><i class="fa fa-map-marker"></i> North Nazimabad, Karachi Sindh</a>
</p>
7-Get keywords from meta tag
"""
# Dataset created using this scraper
# https://www.kaggle.com/asimzahid/pakistans-largest-pakwheels-automobiles-listings
import json
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
# from collections import defaultdict
def get_data(url):
"""
Get web page
"""
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html.parser')
return soup
def get_page_json(soup):
"""
Get json from single page
:Prams: html page got from beautiful soup
:Returns: json based details of the car
"""
try:
# car_json_data = soup.find_all("div", attrs={"class": "row ad-listing-template mt10"})[0].div.script
# print("towards carPageData")
car_page_data = json.loads(
str(
soup.find_all("div", attrs={"class": "row ad-listing-template mt10"})[0].div.script
)
.lstrip("""<script type="application/ld+json">""")
.rstrip("</script>"))
# print("carPageData passed")
return car_page_data
# return car_page_data
# articles_list = soup.findAll('section')
except Exception as e:
print("carPage data exception")
car = {}
car = json.dumps(car)
# car.title = soup.find("meta", property="og:title")["content"]
# print("Title")
# print(car.title)
car.url = soup.find("meta", property="og:url")["content"]
car.images = soup.find("meta", property="og:image")["content"]
car.description = soup.find("meta", property="og:description")["content"]
car["modelDate"], car["mileageFromOdometer"], car["vehicleTransmission"], car["fuelType"], car["vehicleEngine"] = engine_data(soup)
car.offers = offers_data(soup)
print("carPage data exception completed")
print(f"get page data\n{e}")
return car
def get_car_features(soup):
"""
extract car features from
"""
features = []
try:
length = len(soup.find_all("ul", attrs={"class": "list-unstyled car-feature-list nomargin"})[0].find_all("li"))
for each in range(length):
features.append((soup.find_all("ul", attrs={"class": "list-unstyled car-feature-list nomargin"})[0].find_all("li")[each].text).strip())
# print(features)
return features
except Exception as e:
if e == "list index out of range":
return features
else:
return features
def get_few_features(soup):
"""
extract few extra features from
"""
try:
length = len(soup.find_all("ul", attrs={"id": "scroll_car_detail"})[0].find_all("li"))
key = []
value = []
for each in range(length):
if (each%2 == 0):
key.append((soup.find_all("ul", attrs={"id": "scroll_car_detail"})[0].find_all("li")[each].text).replace(' ', ''))
else:
value.append(soup.find_all("ul", attrs={"id": "scroll_car_detail"})[0].find_all("li")[each].text)
return dict(zip(key, value))
except Exception as e:
print(f"extra few features\n{e}")
return ""
def last_updated(extra_features_json):
"""
Last updated Ad
"""
try:
return extra_features_json["LastUpdated:"]
except Exception as e:
print(f"Last Updated\n{e}")
return ""
def seller_loaction(soup):
"""
Extract seller Loaction from sub heading
"""
try:
return soup.find_all("p", attrs={"class": "detail-sub-heading"})[0].a.text
except Exception as e:
print(f"seller location\n{e}")
return ""
def ad_posting_platform(soup):
"""
Extract Ad posting platform from sub heading
"""
try:
return soup.find_all("p", attrs={"class": "detail-sub-heading"})[0].span.text
except AttributeError:
return "Added via Website"
except Exception as e:
print("ad_posting_platform\n")
print(e)
def get_keywords(soup):
"""
Extract vehicle keywords from meta information
"""
return soup.find("meta", attrs={"name":"keywords"})["content"]
def engine_data(soup):
"""
extract engine data
"""
# car = {}
for each in range(3):
engine = (soup.find_all("ul", attrs={"class": "list-unstyled ad-specs list-inline pull-left nomargin"})[0].find_all("li")[each].text).strip()
if each == 0:
modelDate = engine
elif each == 1:
mileageFromOdometer = engine
elif each == 2:
engine_split = [x.strip() for x in engine.split('.')]
print(engine_split)
vehicleTransmission = engine_split[-1]
fuelType = engine_split[0]
vehicleEngine = {
"@type": "EngineSpecification",
"engineDisplacement": engine_split[1]
}
return modelDate, mileageFromOdometer, vehicleTransmission, fuelType, vehicleEngine
def save_list(json_list):
"""
Save data into file
"""
try:
data = {"usedCars": json_list}
with open('usedCar.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
except Exception as e:
print(f"save\n{e}")
def save_each_page(json_list):
"""
Save data into file
"""
try:
previous_data = open_json_list()
previous_data += json_list
data = {"usedCars": previous_data}
with open('usedCar.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
except Exception as e:
print(f"save each page\n{e}")
def open_json_list():
with open('usedCar.json') as f:
data = json.load(f)
data_list = data["usedCars"]
return data_list
def offers_data(soup):
"""
offers data
"""
price = (soup.find_all("div", attrs={"class": "price-box"})[0].strong.text).replace("PKR","").strip()
offers = {
"price": price,
"priceCurrency": "PKR",
}
return offers
def add_features(soup, car):
"""
features agregator method
"""
try:
try:
car["sellerLocation"] = seller_loaction(soup)
except Exception as e:
print("Error: sellerLocation")
try:
car["postedFrom"] = ad_posting_platform(soup)
except Exception as e:
print("Error: postedFrom")
try:
extra_features = get_few_features(soup)
except Exception as e:
print("Error: extra features")
# print(extra_features)
try:
car["keywords"] = get_keywords(soup)
except Exception as e:
print("Error: Keywords")
try:
car["extraFeatures"] = extra_features
except Exception as e:
print("Error: EXTRA FEATURES")
try:
car["features"] = get_car_features(soup)
except Exception as e:
print("features")
try:
car["adLastUpdated"] = last_updated(extra_features)
except Exception as e:
print(f"last update\n{e}")
return car
except Exception as e:
print(f"add Features\n{e}")
def main():
"""
Pagination
"""
page_counter = 1
# pages_list = []
while page_counter != 4575:
article_page = 'https://www.pakwheels.com/used-cars/search/-/?page=' + str(page_counter)
print(f"Page Number: {page_counter}")
try:
req = requests.get(article_page)
page_counter += 1
soup = BeautifulSoup(req.content, 'html.parser')
articles_list = soup.findAll('section') # [1].div.script
# print(articles_list[0].div.script)
page_data = json.loads(str(articles_list[0].div.script).lstrip("""<script type="application/ld+json">""").rstrip("</script>"))
# print(page_data['url'])
page_url_list = page_data['itemListElement']
all_car = []
for each in tqdm(page_url_list):
try:
# print(each["url"])
# print(each["url"])
try:
soup = get_data(each["url"])
except Exception as e:
print(f"get_data\n{e}")
try:
car = get_page_json(soup)
except Exception as e:
print(f"get_page_data\n{e}")
try:
car = add_features(soup, car)
except Exception as e:
print(f"add_features_loop\n{e}")
# offers = "@offers"
car["price"] = car["offers"]["price"]
car["priceCurrency"] = car["offers"]["priceCurrency"]
# car["url"] = car["offers"]["url"]
car["image"] = each["image"]
car["name"] = each["name"]
car.pop("@context")
car.pop("offers")
# print(car)
if car != []:
all_car += [car]
except Exception as e:
print(f"Each Page\n{e}")
save_each_page(all_car)
print("Page completed")
except Exception as e:
print(e)
# save_list(all_car)
if __name__ == "__main__":
# url = "https://www.pakwheels.com/used-cars/daihatsu-mira-2018-for-sale-in-islamabad-5243343"
# "https://www.pakwheels.com/used-cars/suzuki-alto-2019-for-sale-in-karachi-5460527"
main()
# urls_list = open_json_list()
# all_car = []
# for each_url in tqdm(urls_list):
# try:
# soup = get_data(each_url)
# car = get_page_json(soup)
# car = add_features(soup, car)
# try:
# print("merge")
# all_car += [car]
# print("Merger done")
# except Exception as e:
# print(f"merge List\n{e}")
# except Exception as e:
# print(f"Last\n{e}")
# # print(all_car)
# save_list(all_car)