· 6 years ago · Jul 18, 2019, 12:04 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 with open('images/' + str(img_num) + ".jpg", 'wb') as handler:
49 handler.write(img_data)
50 r = rr.text
51 itemPage = BeautifulSoup(r, 'html.parser')
52 serviceList = itemPage.select(
53 # "body > div.main-container > div > div.columns > div.left.top > div > div.repair-services > div.container-md > div.repair-services-list > table > tbody > tr")
54 "div > div.columns > div.left.top > div > div.repair-services > div.container-md > div.repair-services-list > table > tbody > tr")
55 # print(serviceList.__str__())
56 for service in serviceList:
57 # print(service.text)
58 sss = BeautifulSoup(str(service), 'html.parser')
59 price = str(sss.select_one('td.pricing')).replace('<td class="pricing">', '').replace('</td>', '')
60 if (price.startswith('<td class="pricing blink-element blink-group"')):
61 continue
62 name = sss.select_one("div.form-check").text
63
64 name = str(name).strip()
65
66 # print(name + " " + price)
67
68 price_new = float(price) - (float(price) / 100 * 10)
69
70 db.execute(
71 "INSERT INTO services VALUES ('" + str(phone) + "', '" + str(img_num) + ".jpg', " + str(
72 price) + ", " + str(
73 int(price_new)) + ", '" + sss.select_one("td").text + "');")
74 db.commit()
75 img_num = img_num + 1
76db.close()