-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmw.py
More file actions
119 lines (102 loc) · 3.95 KB
/
Copy pathbmw.py
File metadata and controls
119 lines (102 loc) · 3.95 KB
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
# Webscraping data
# import libraries
import urllib3
from bs4 import BeautifulSoup
import time
import certifi
import csv
import numpy as np
# Start time
start_time = time.time()
#Create storage lists
prices = []
kms = []
year = []
# Initiating a for loop to fill the sorage lists with raw data per page
for i in range(1,21):
# specify the url
url_base = "https://www.autoscout24.nl/resultaten?mmvmk0=13&mmvmd0=18481&mmvco=1&cy=NL&fuel=D&powertype=kw&atype=C&ustate=N%2CU&sort=standard&desc=0&page="
url = url_base + str(i) + '&size=20'
print('extracting information from page '+ str(i))
# query the website and return parsed data into variable 'soup'
http = urllib3.PoolManager(10, ca_certs=certifi.where())
response = http.request('GET', url)
soup = BeautifulSoup(response.data.decode('utf-8'), "html.parser")
# LISTING PRICES----------------------------------------------------------------
# Find all price locations and store them in price_loc
price_loc = soup.findAll("span", {"class": "cldt-price sc-font-xl sc-font-bold", "data-item-name": "price"})
# Loop through each element in price_loc
for each in price_loc :
# Strip each price from location and store them in the list prices
prices.append(each.text.strip()) # strip() is used to remove starting and trailing
# LISTING KMs and year----------------------------------------------------------
# Create temporary storage list
li_kms_year = []
# Find list locations containing kms and year and store in li_loc
li_loc = soup.findAll("ul", {"data-item-name": "vehicle-details"}) # NOTE: selects whole li!
# Loop through each element in li_loc
for each in li_loc :
# Appends every li to combined li_kms_year list
li_kms_year.append(each.text.strip())
# Extracting kms and year from the li_kms_year list
i = 0
while i < len(li_kms_year) : # NOTE: EOF technique
# split list in var for car i and extract kms and year, appending to the lists
var = li_kms_year[i].split("\n\n\n")
kms.append(var[0])
year.append(var[1])
i += 1
# Wait 1 second before opening new page
time.sleep(1)
#Cleaning prices
prices_clean1 = ''.join(''.join(prices).split('€ ')).split(',-')
prices_clean2= []
for each in prices_clean1 :
if len(each) < 9 :
prices_clean2.append(each)
else :
prices_clean2.append(each[17:])
del(prices_clean2[-1])
prices_clean3=[]
for each in prices_clean2 :
prices_clean3.append(each.replace('.',''))
# Converting string into int
clean_prices = list(map(int,prices_clean3))
#Cleaning kms
clean_kms1 = []
for each in kms :
clean_kms1.append(each[:-3])
clean_kms2 = []
for each in clean_kms1 :
clean_kms2.append(each.replace('.',''))
# Converting string into int
clean_kms = list(map(int,clean_kms2))
# Cleaning year
clean_year1 = []
for each in year :
clean_year1.append(each[3:])
# Finding incomplete datapoints
# Indexlist incomplete datapoints
indexes_missing_data = []
for index, year in enumerate(clean_year1) :
if year == ' (Bouwjaar)':
indexes_missing_data.append(str(index))
# Converting string into int
clean_missing = list(map(int,indexes_missing_data))
# Using the indexes to delete corresponding data points in the lists
for i in clean_missing :
i = i - clean_missing.index(i)
del(clean_year1[i])
del(clean_kms[i])
del(clean_prices[i])
# Converting string year into int
clean_year = list(map(int,clean_year1))
# Combining the clean data, converting to np and transposing the columns
combined_list = np.transpose(np.array([clean_year, clean_kms, clean_prices]))
# Printing update about the number of cars found
print('extracted information of ' + str(len(clean_year)) + ' cars, in ' + '%s seconds' % round((time.time() - start_time), 0))
# Exporting data to .csv file
with open('/Users/bastiaanwitte/desktop/datascience/bmw.csv', "w") as output:
writer = csv.writer(output)
writer.writerows(combined_list)
# End of script