-
Notifications
You must be signed in to change notification settings - Fork 2
/
polovniAutomobili.py
211 lines (175 loc) · 4.76 KB
/
polovniAutomobili.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
#!/bin/bash/env python
import requests
import urllib.request
from bs4 import BeautifulSoup as bs
import mysql.connector
# function that creates database on local server
def createDB():
mydb = mysql.connector.connect(
host='localhost',
user='root',
passwd=''
)
#database='testDB'
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE polovniAutomobili")
# check if database exists
#mycursor.execute("SHOW DATABASES")
#for db in mycursor:
# print(db)
# parse through page using BeautifulSoup
def parse_page(url):
try:
r = requests.get(url)
soup = bs(r.text, 'html.parser')
return soup
except:
pass
# returns a list of all models of one brand
def get_models(brand):
brand = (brand.replace(' ', '-')).lower()
url ='https://www.polovniautomobili.com/auto-oglasi/pretraga?brand=' + brand
soup = parse_page(url)
models= soup.find(id='model')
models_list=[]
for model in models:
if model.get_text()=='Ostalo':
pass
else:
models_list.append(model.get_text())
return models_list[1:]
# returns a list of all brands
def all_Brands():
url = 'https://www.polovniautomobili.com/#'
soup = parse_page(url)
brands = soup.find(id='brand')
brands_list = []
for brand in brands:
brands_list.append(brand.get_text())
return brands_list[1:]
# function that replaces characters in string
def replace(string):
string = string.replace('š','s')
string = string.replace('Š','s')
string = string.replace('ć','c')
string = string.replace('Ć','c')
string = string.replace('Ž','z')
string = string.replace('ž','z')
string = string.replace('č','c')
string = string.replace('Č','c')
return string
# get all cars from one brand and model
def get_cars(brand, model):
brand = (brand.replace(' ', '-')).lower()
model = (model.replace(' ', '-')).lower()
url ='https://www.polovniautomobili.com/auto-oglasi/pretraga?brand=' + brand + '&model[]=' + model
#soup = parse_page(url)
carList = []
# loop over number of pages
for i in range(1,2):
#print('page='+str(i))
#print("")
soup = parse_page(url+'&page='+str(i))
#print(soup)
if soup!=None:
pages = soup.findAll('article')
for page in pages:
#print(page)
pageT = page.find('a')
try:
title = pageT.get('title')
link = pageT.get('href')
if title==None:
pass
else:
#print(title)
discount = page.find(class_='price price-discount')
if discount!=None:
price = discount.get_text()
continue
else:
price = page.find(class_='price')
price = price.get_text()
content = page.findAll(class_='inline-block')
blocks = []
for con in content:
blocks.append(con.get_text())
god = int(blocks[0][:4])
km = blocks[1].replace('.','')
km = km.replace(' km |', '')
km = int(km)
gor = str(blocks[2][:-2])
gor = replace(gor)
kub = int(blocks[3][:4])
kar = (blocks[4].replace(',',''))
kar = replace(kar)
sn = (blocks[5].replace(', ',''))
try:
price = int(price[:-2].replace('.',''))
dictionary = {
'Brend': (brand.replace('-', ' ')).upper(),
'Model': (model.replace('-', ' ')).upper(),
'Naziv': replace(title),
'Cena': price,
'Godiste': god,
'Kilometraza':km,
'Gorivo':gor,
'Kubikaza':kub,
'Karoserija':kar,
'Snaga': sn,
'Link': link
}
# append every car to list as dictionary
carList.append(dictionary)
except:
pass
except Exception as e:
print(e)
pass
return carList
# function that inserts cars into database
def insertDBAll():
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="polovniAutomobili"
)
mycursor = mydb.cursor()
drop = 'DROP TABLE Cars'
mycursor.execute(drop)
mycursor.execute("CREATE TABLE Cars (brend VARCHAR(255), model VARCHAR(255)\
,naziv VARCHAR(255), cena INT(10), godiste INT(10), kilometraza INT(10)\
,gorivo VARCHAR(255), kubikaza INT(10), karoserija VARCHAR(255)\
,snaga VARCHAR(255))")
sqlInsert = "INSERT INTO Cars (brend, model, naziv, cena, godiste, kilometraza,\
gorivo, kubikaza, karoserija, snaga) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
# insert cars by brand and model
brands = all_Brands()
try:
for brand in brands:
models = get_models(brand)
for model in models:
if model==None:
pass
else:
cars = get_cars(brand, model)
for car in cars:
if car==None:
pass
else:
values = tuple(car.values())
try:
mycursor.execute(sqlInsert, values)
mydb.commit()
except:
pass
except Exception as e:
#print(values)
print(e)
pass
# create database and then insert data
#createDB()
#insertDBAll()
print("Data inserted")