-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
187 lines (177 loc) · 6.37 KB
/
index.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
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import array
import json
import re
# schema
# {
# number: {type: Number,},
# title: {type: String},
# subtitle: {type: String},
# details: {type: String}, // short details
# category: {type: String},
# price: {type: String},
# rating: {type: String},
# date: {type: String},
# coverImage: {type: String},
# slides: [{type: String}],
# titleLink: {type: String},
# subtitleLink: {type: String},
# fileLink: {type: String},
# imageLink: {type: String},
# description: {type: String}, // long description
# others: {type: String},
# copyrightInfo: {type: String}
# }
# {
# title: string,
# subtitle: string,
# description: string,
# category: string,
# subcategory: string,
# typeMain: number,
# date: date,
# covers: [string],
# copyrightInfo: string,
# sources: [string],
# tags: [string],
# data: [
# {
# data: string,
# link: string,
# type: number
# }
# ]
# }
# def scrap_item(url):
# url_req = uReq(url)
# url_read = url_req.read()
# url_req.close()
# temp = {}
# page = soup(url_read,"html.parser")
# page_info = page.find("article",{"class":"news-article"})
# title = page_info.header.h1.string
# print("title: ", title)
# author_list = []
# for author in page_info.find_all("span",{"class":"by-author"}):
# author_list.append(author.text.strip().replace(',',''))
# print("authors: ", author_list)
# date = page_info.find("time",{"class":"relative-date"}).get('datetime')
# print("date: " ,date)
# category = page_info.find("a",{"class":"category"}).string.strip()
# print("category: ", category)
# covers = page_info.find('img').get('src')
# print("covers: ",covers)
# for paragraph in pageinfo.find_all('p'):
# if(paragraph.)
# temp['text'] = content.text.strip()
# print(temp['text'])
# if(content.find_all('img')):
# temp['image']= []
# for pic in content.find_all('img'):
# temp['image'].append(pic.get('src'))
# if(content.find_all('iframe')):
# temp['videos']= []
# for vid in content.find_all('iframe'):
# temp['videos'].append(vid.get('src'))
# if(page_info.h1):
# temp['title'] = page_info.h1.text
# if(page_info.find("p",{"class":"s_date"}).timcleare):
# temp['date'] = page_info.find("p",{"class":"s_date"}).time.text
# if(page_info.find("p",{"class":"s_date"})):
# dummy = page_info.find("p",{"class":"s_date"}).text.split("by")
# temp['author'] = dummy[len(dummy)-1].strip()
# return temp
myurl = "https://www.techradar.com/news/phone-and-communications/mobile-phones/best-cheap-smartphones-payg-mobiles-compared-1314718"
something = uReq(myurl)
html_page = something.read()
something.close()
#html parsing
soup_page = soup(html_page, "html.parser")
header = soup_page.find("header")
content = soup_page.find("")
if(header.h1.string):
title = header.h1.string
author_list = []
for author in header.find_all("span",{"class":"by-author"}):
if(author):
author_list.append(author.text.strip().replace(',',''))
if(header.div.h2.string):
subtitle = header.div.h2.string
if(header.find("time")):
time = header.find("time").string.strip()
if(header.find("a",{"class":"category"})):
category = header.find("a",{"class":"category"}).string.strip()
# print(title)
# print(subtitle)
# print(author_list)
# print(time)
# print(category)
if(soup_page.find("div",{'class':'box'}).img):
covers = [soup_page.find("div",{'class':'box'}).img["src"]]
print('COVERS: ',covers)
phone_list = []
content = soup_page.find("div",{'id':'article-body'})
for phone_title in content.find_all('h3'):
if(phone_title.previous_sibling.img):
imageLink = phone_title.previous_sibling.img['data-src']
print ("IMAGE SRC: ",imageLink)
title = phone_title.text.strip()
print("TITLE: ",title)
iterator = phone_title.next_sibling
advan = []
disadvan = []
text =""
while( iterator.next_sibling != None and iterator.next_sibling.name != 'h3'):
if(iterator.name == 'aside'):
price = iterator.find("div",{"class":"hawk-price-deal-price-container"}).text
print("PRICE: ",price)
if(iterator.has_attr("class")):
if("subtitle" in iterator['class']):
subtitle = iterator.text.strip()
print("SUBTITLE: " ,subtitle)
if(iterator.name == 'p' and iterator.previous_sibling.has_attr('class')):
if('subtitle' in iterator.previous_sibling['class']):
details = iterator.text.strip()
print("DETAILS: ",details)
if(iterator.name =='div' and 'icon-plus_circle' in iterator['class']):
advan.append(iterator.text.strip())
# print("ADVANTAGES: ", advan)
if(iterator.name =='div' and 'icon-minus_circle' in iterator['class']):
disadvan.append(iterator.text.strip())
# print("DISADVANTAGES: ",disadvan)
elif(iterator.name =='p' and iterator.text!=None):
if(not "Read the full review" in iterator.text):
text = text + iterator.text
iterator = iterator.next_sibling
print("ADVANTAGES: ", advan)
print("DISADVANTAGES: ",disadvan)
print("TEXT DETAILS: ",text)
# print(subtitle)
print("\n\n")
# print("\n\n")
# print("\n\n")
# print("\n\n")
continue
# phone={}
# # if(phone_title.previous_sibling.find("img")):
# # imageLink = []
# # for images in phone_title.previous_sibling.img['src']:
# # imageLink.append(images['src'])
# # phone['imageLink'] = imageLink
# print(phone_title.previous_sibling.find('img')['src'])
# iterator = phone_title.next_sibling
# phone['title'] = phone_title.text.strip()
# if('subtitle' in iterator['class']):
# phone['subtitle'] = iterator.text.strip()
# iterator = phone_title.next_sibling
# # print (phone['imageLink'])
# print (phone['title'])
# # print (phone['imageLink'])
# if("subtitle" in iterator['class']):
# list_subtitle = iterator.text.strip()
# iterator = iterator.next_sibling
# for subt in content.find_all('div',{'class':'subtitle'}):
# print ("SUBTITLE: ",subt.p.text)
# if(subt.next_sibling.name == 'p'):
# print ("DETAILS :",subt.next_sibling.text)