-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
executable file
·282 lines (238 loc) · 10.4 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import csv
import os
import logging
from bs4 import BeautifulSoup
import cloudscraper
import argparse
keywords = {"mileage": ["miles"],
"BHP": ["BHP"],
"transmission": ["Automatic", "Manual"],
"fuel": ["Petrol", "Diesel", "Electric", "Hybrid – Diesel/Electric Plug-in", "Hybrid – Petrol/Electric",
"Hybrid – Petrol/Electric Plug-in"],
"owners": ["owners"],
"body": ["Coupe", "Convertible", "Estate", "Hatchback", "MPV", "Pickup", "SUV", "Saloon"],
"ULEZ": ["ULEZ"],
"year": [" reg)"],
"engine": ["engine"]}
def get_car_details(article):
try:
seller_info = article.find("h3", {"class": "product-card-seller-info__name atc-type-picanto"}).text.strip()
if "Private" in seller_info:
price_indicator = ""
else:
price_indicator = article.find("li", {"class": "badge-group__item"}).text.strip()
except Exception as e:
price_indicator = ""
pass
#
if article.find("p", {"class": "product-card-details__attention-grabber"}):
try:
highlight = article.find("p", {"class": "product-card-details__attention-grabber"}).text.strip()
highlight.encode('utf-8', 'surrogateescape').decode('utf-8', 'replace')
except Exception as e:
highlight = ""
pass
highlight_info = highlight
else:
highlight_info = ""
car = {
"name": article.find("h3", {"class": "product-card-details__title"}).text.strip(),
"link": "https://www.autotrader.co.uk" + article.find("a", {"class": "tracking-standard-link"})["href"][: article.find("a", {"class": "tracking-standard-link"})["href"].find("?")],
"price": article.find("div", {"class": "product-card-pricing__price"}).text.strip().replace(",", ""),
"subtitle": article.find("p", {"class": "product-card-details__subtitle"}).text.strip().replace(",", ""),
"highlight": highlight_info.replace(",", ""),
"price-indicator": price_indicator,
"seller-info": article.find("h3", {"class": "product-card-seller-info__name atc-type-picanto"}).text.strip().replace(",", ""),
}
key_specs_bs_list = article.find("ul", {"class": "listing-key-specs"}).find_all("li")
for key_spec_bs_li in key_specs_bs_list:
key_spec_bs = key_spec_bs_li.text
if any(keyword in key_spec_bs for keyword in keywords["mileage"]):
car["mileage"] = int(key_spec_bs[:key_spec_bs.find(" miles")].replace(",", ""))
elif any(keyword in key_spec_bs for keyword in keywords["BHP"]):
car["BHP"] = int(key_spec_bs[:key_spec_bs.find("BHP")])
elif any(keyword in key_spec_bs for keyword in keywords["transmission"]):
car["transmission"] = key_spec_bs
elif any(keyword in key_spec_bs for keyword in keywords["fuel"]):
car["fuel"] = key_spec_bs
elif any(keyword in key_spec_bs for keyword in keywords["owners"]):
car["owners"] = int(key_spec_bs[:key_spec_bs.find(" owners")])
elif any(keyword in key_spec_bs for keyword in keywords["body"]):
car["body"] = key_spec_bs
elif any(keyword in key_spec_bs for keyword in keywords["ULEZ"]):
car["ULEZ"] = key_spec_bs
elif any(keyword in key_spec_bs for keyword in keywords["year"]):
car["year"] = key_spec_bs.split(' ')[0]
elif key_spec_bs[1] == "." and key_spec_bs[3] == "L":
car["engine"] = key_spec_bs
else:
logging.info(f'Unidentified information {key_spec_bs}')
return car
def get_page_html(url, scraper, params={}, max_attempts_per_page=5):
attempt = 1
while attempt <= max_attempts_per_page:
r = scraper.get(url, params=params)
logging.info(f"Response: {r}")
if r.status_code == 200:
first_character = r.text[0]
if first_character == '{':
page_html = r.json()["html"]
elif first_character == '<':
page_html = r.text
else:
raise Exception(f'Unknown start to response from {r.url}: {r.text[:100]}')
s = BeautifulSoup(page_html, features="html.parser")
return s
else: # if not successful (e.g. due to bot protection), log as an attempt
attempt = attempt + 1
logging.info(f"Exception. Starting attempt #{attempt} ")
logging.info(f"Exception. All attempts exhausted for this page. Skipping to next page")
return None
def get_cars(json_data, verbose=False):
#
max_attempts_per_page=5
if verbose:
log_level = logging.INFO
else:
log_level = logging.WARNING
logging.basicConfig(format='%(levelname)s:%(message)s', level=log_level)
# To bypass Cloudflare protection
scraper = cloudscraper.create_scraper()
# Basic variables
results = []
n_this_year_results = 0
url_default = "https://www.autotrader.co.uk/car-search"
#
# Set up parameters for query to autotrader.co.uk
search_params = {
"sort": "relevance",
"postcode": json_data["postcode"],
"radius": json_data["radius"],
"make": json_data["make"],
"model": json_data["model"],
"ma": json_data["manufacturer_approved"],
"maximum-mileage": json_data["maximum-mileage"],
"price-to": json_data["max_price"],
"aggregatedTrim": json_data["model_variant"],
"search-results-price-type": "total-price",
"search-results-year": "select-year",
}
if json_data["include_writeoff"] == "include":
search_params["writeoff-categories"] = "on"
elif json_data["include_writeoff"] == "exclude":
search_params["exclude-writeoff-categories"] = "on"
elif json_data["include_writeoff"] == "writeoff-only":
search_params["only-writeoff-categories"] = "on"
year = json_data["min_year"]
page = 1
try:
while year <= json_data["max_year"]:
search_params["year-from"] = year
search_params["year-to"] = year
logging.info(f"Year: {year}\nPage: {page}")
url = url_default
params = search_params
try:
while url:
s = get_page_html(url, scraper, params=params, max_attempts_per_page=max_attempts_per_page)
if s:
articles = s.find_all("article", attrs={"data-standout-type": ""})
next_page_object = s.find(attrs={"class": "pagination--right__active"})
else:
articles = []
next_page_object = None
for article in articles:
car = get_car_details(article)
results.append(car)
n_this_year_results = n_this_year_results + 1
if next_page_object:
page = page + 1
url = next_page_object['href']
params = {}
logging.info(f"Car count: {len(results)}")
logging.info("---------------------------------")
else:
url = None
logging.info(f"Found total {n_this_year_results} results for year {year} across {page} pages")
except KeyboardInterrupt:
break
# Increment year and reset relevant variables
year = year + 1
page = 1
n_this_year_results = 0
if year <= json_data["max_year"]:
logging.info(f"Moving on to year {year}")
logging.info("---------------------------------")
except KeyboardInterrupt:
pass
return results
### Output functions ###
def save_csv(filename,results=None):
csv_columns = ["name", "link", "price","subtitle","highlight","price-indicator","seller-info","mileage", "BHP", "transmission", "fuel", "owners", "body", "ULEZ",
"engine", "year"]
if results:
with open(filename, "w", newline='') as f:
writer = csv.DictWriter(f, fieldnames=csv_columns)
writer.writeheader()
for data in results:
writer.writerow(data)
def save_json(filename,results=None ):
if results:
with open(filename, 'w') as f:
json.dump(results, f, sort_keys=True, indent=4, separators=(',', ': '))
def parse_args():
sample_input_json_file = """
{
"make" : "Audi",
"model" : "A5",
"model_variant" : "",
"manufacturer_approved" : "",
"maximum-mileage": 40000,
"max_price": 30000,
"postcode" : "SW1A 0AA",
"radius" : 1500,
"min_year" : 2005,
"max_year" : 2020,
"include_writeoff" : "include",
"max_attempts_per_page" : 5,
"verbose" : "False"
}
"""
parser = argparse.ArgumentParser()
parser.add_argument("--type", help="output type csv or json", default="csv")
parser.add_argument("--outfile", help="file prefix", default="scraper_output")
parser.add_argument("--verbose", help="verbose mode", default=False)
parser.add_argument("--inputfile", help="input file in json", default=sample_input_json_file)
args = parser.parse_args()
if not args.inputfile:
print("ERROR: --inputfile is required ")
return args
def main():
args=parse_args()
input_file = args.inputfile
json_data = {}
#
if (os.path.exists(input_file)):
with open(input_file,'r') as file:
json_data = json.load(file)
else:
json_data = input_file
#
results = get_cars(json_data,args.verbose)
if args.type == "json":
if not ".json" in args.outfile:
out_file = "{0}.{1}".format(args.outfile,args.type)
else:
out_file = args.outfile
save_json(out_file,results)
else:
if not ".csv" in args.outfile:
out_file = "{0}.{1}".format(args.outfile,args.type)
else:
out_file = args.outfile
save_csv(out_file,results)
if __name__ == "__main__":
main()