· 6 years ago · Jul 24, 2019, 03:12 PM
1import requests
2from bs4 import BeautifulSoup
3import sqlite3
4import os
5
6tuple_of_brand = (
7 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhyvanie-iphone',
8 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhyvanie-htc',
9 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhyvanie-samsung',
10 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-nokia',
11 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-sony',
12 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-motorola',
13 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-lg',
14 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-huawei',
15 'https://restore.com.ua/remont-mobilnyh-telefonov/blackberry',
16 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-vertu',
17 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-meizu',
18 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-zte',
19 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-lenovo',
20 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-asus',
21 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-oneplus',
22 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-google',
23 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-vivo',
24 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-telefonov-xiaomi',
25 'https://restore.com.ua/remont-mobilnyh-telefonov/remont-i-obsluzhivanie-acer'
26)
27
28db = sqlite3.connect("items.db")
29db.execute(
30 '''create table if not exists services (device varchar(256) not null, image_name varchar(12) not null,price_original int not null,price_new int not null,service varchar(256) not null);''')
31for ll in tuple_of_brand:
32 link = requests.get(ll)
33 soup = BeautifulSoup(link.text, 'html.parser')
34 img_num = 0
35 links = soup.select(
36 "body > div.main-container.page-container > div > div.container.models-list-container > div > a")
37 for link in links:
38 if os._exists('images/' + str(img_num) + ".jpg"):
39 os.remove('images/' + str(img_num) + ".jpg")
40 # time.sleep(0.5)
41 phone = link.text
42 # print(
43 # phone + " ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
44 rr = requests.get("https://restore.com.ua" + link['href'])
45 img_data = requests.get('https://restore.com.ua' + BeautifulSoup(rr.text, 'html.parser').select_one(
46 'div > div.columns > div.left.top > div > div.repair-services > div.repair-device-header > div > img')[
47 'src']).content
48 path = "images"
49 try:
50 os.mkdir(path)
51 except:
52 pass
53 # else:
54 # print("Successfully created the directory %s " % path)
55 with open('images/' + str(img_num) + ".jpg", 'wb') as handler:
56 handler.write(img_data)
57 r = rr.text
58 itemPage = BeautifulSoup(r, 'html.parser')
59 serviceList = itemPage.select(
60 # "body > div.main-container > div > div.columns > div.left.top > div > div.repair-services > div.container-md > div.repair-services-list > table > tbody > tr")
61 "div > div.columns > div.left.top > div > div.repair-services > div.container-md > div.repair-services-list > table > tbody > tr")
62 # print(serviceList.__str__())
63 for service in serviceList:
64 # print(service.text)
65 sss = BeautifulSoup(str(service), 'html.parser')
66 price = str(sss.select_one('td.pricing')).replace('<td class="pricing">', '').replace('</td>', '')
67 if (price.startswith('<td class="pricing blink-element blink-group"')):
68 continue
69 name = sss.select_one("div.form-check").text
70
71 name = str(name).strip()
72
73 # print(name + " " + price)
74
75 price_new = float(price) - (float(price) / 100 * 10)
76
77 db.execute(
78 "INSERT INTO services VALUES ('" + str(phone) + "', '" + str(img_num) + ".jpg', " + str(
79 price) + ", " + str(
80 int(price_new)) + ", '" + sss.select_one("td").text + " (" + str(phone) + ")');")
81 db.commit()
82 img_num = img_num + 1
83db.close()