-
Notifications
You must be signed in to change notification settings - Fork 2
/
Specifications.py
67 lines (51 loc) · 2.12 KB
/
Specifications.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
import json
from request import Request
from mongo import MongoDB
class Specifications:
""""
get specification of each mobile
"""
@staticmethod
def get_specifications():
mongo = MongoDB()
database = mongo.database
images = []
# you have to open the file of each brand for get their mobile specification
with open('link/samsung.json', 'r') as s:
link = json.loads(s.read())
for li in link:
document = {}
tables = {}
features = {}
url = f"https://www.gsmarena.com/{li}"
response = (Request.request(url))
phone_name = response.find('h1').text
if '.' in phone_name:
phone_name = ''.join(phone_name.split('.'))
print(phone_name)
image = response.find('div', {'class': 'specs-photo-main'})
photo_link = (image.find('img').get('src'))
images.append(photo_link)
print(photo_link)
find_div = response.find('div', {'id': 'specs-list'})
find_table = find_div.findAll('table')
for table in find_table:
table_name = table.find('th', {'scope': 'row'}).text
for tr in table.find_all('tr'):
td_ttl = getattr(tr.find('td', {'class': 'ttl'}), 'text', None)
if td_ttl is None:
td_ttl = 'sample'
elif '.' in td_ttl:
td_ttl = ''.join(td_ttl.split('.'))
td_nfo = getattr(tr.find('td', {'class': 'nfo'}), 'text', None)
features.update({td_ttl: td_nfo})
tables.update({table_name: features})
features = {}
document.update({'phone': phone_name})
document.update({'features': tables})
print(document)
collections = database['samsung1']
collections.insert_one(document)
# this file is for images of main page of each mobile
with open('image/samsung-image.json', 'w') as s:
s.write(json.dumps(images))