-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.py
45 lines (44 loc) · 1.96 KB
/
Car.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
import mysql.connector
import requests
import bs4
import DBConfig
from DBConfig import mysq_useranem
from DBConfig import mysql_password
########### creating DB and table #########
def CAR_data_fetching():
DBConfig.HOME_CAR_creating()
DBConfig.CAR_table_creating()
############ Connecting to DB #############
dbconnector = mysql.connector.connect(host="127.0.0.1",
user="%s" % mysq_useranem,
password="%s" % mysql_password,
database="H_C",
table="car_info"
)
my_cursor = dbconnector.cursor()
########### Connecting to site ############
id = 0
id2 = 0
for page in range(291):
url = "https://takhtegaz.com/car/sale?page=%i" % page
response = requests.get(url)
soup = bs4.BeautifulSoup(response.content, 'html.parser')
########### insert to DB #################
for m in soup.find_all('h3', attrs={"class":"card-main__title"}):
temp = m.get_text().strip()
year = temp[0:5]
model = temp[5:len(temp)-11]
my_cursor.execute("INSERT INTO car_info(model,year_car) VALUES(\'%s\', \'%s\');" % (model, year))
for kc in soup.find_all('ul', attrs={"class":"specs hidden-md-down clearfix"}):
id += 1
templs = kc.get_text().strip().split()
karkard = templs[0]
color = templs[3]
my_cursor.execute("UPDATE car_info SET karkard = \'%s\', color = \'%s\' WHERE ID = \'%d\';" % (karkard, color, id))
for p in soup.find_all('div', attrs={"class":"card__price money-format"}):
id2 += 1
price = p.get_text().strip(" تومان")
my_cursor.execute("UPDATE car_info SET prcie = \'%s\' WHERE ID = \'%d\'" % (price, id2))
dbconnector.commit()
dbconnector.close()
my_cursor.close()