forked from starsnotinjars/Automatic-Website-Categorizers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregators.py
165 lines (123 loc) · 3.17 KB
/
aggregators.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
import requests
#from beautifulsoup4 import BeautifulSoup
from bs4 import BeautifulSoup
import re
import os
urls=[]
agg_list=[]
not_agg_list=[]
pin1 = open('websites.txt','r')
lines1 = pin1.readlines();
for line in lines1:
values1 = re.split('', line);
values1[0] = values1[0].replace("\n", "")
urls.append('http://www.'+values1[0])
#print (urls)
# print (urls)
for site in range(0,len(urls)):
url=urls[site]
agg_counts=[]
soup = BeautifulSoup(requests.get(url).text, "html.parser")
target = open('o.txt', 'w')
target.write(str(soup))
target.close()
pin1 = open('o.txt','r')
lines1 = pin1.readlines();
on=0
start=0
text=[]
text_master=[]
a=0
for line in lines1:
values1 = re.split('\s|=|"', line);
for i in range(0,len(values1)):
if values1[i]=='<a' and values1[i+1]=='href':
on=1
if len(values1[i])>1:
if values1[i][0]=='>' and on==1:
start=1
if len(values1[i])>1:
if values1[i][1]=='<':
on=0
start=0
if start==1:
text.append(values1[i])
if len(values1[i])>0:
if values1[i][len(values1[i])-2]=='a' and values1[i][len(values1[i])-1]=='>':
values1[i]=values1[i][:-4]
del text[-1]
text.append(values1[i])
temp=text[0]
temp=temp[1:]
del text[0]
text.insert(0,temp)
on=0
start=0
if len(text)>8:
text_master.append(text)
text=[]
break;
s=" "
first_links=[]
first_titles=[]
strings=[]
for i in range(0, len(text_master)-1):
first_title=[]
string=' '.join(text_master[i])
strings.append(string)
os.system('GoogleScraper -m http -s "google" --keyword "'+str(string)+'" > output'+str(i)+'.txt')
count=0
on=0
on2=0
temp=[]
target = open('output_from_search10.txt', 'w')
pin1 = open('output'+str(i)+'.txt','r')
lines1 = pin1.readlines();
for line in lines1:
values1 = re.split('\s+', line);
# print (values1)
if values1[1]=="'query':":
on=1
count=0
# if on==1 and values1[1]=="'snippet':":
on=2
if on==2:
for i in range(0, len(values1)):
if values1[i]=="'link':":
# print(values1)
first_links.append(values1[i+1])
# break;
for i in range(0, len(values1)):
if values1[i]=="'visible_link'":
break;
if on==3:
first_title.append(values1[i])
if values1[i]=="'title':":
on=3
first_titles.append(first_title)
#print ("first links",first_links)
#print ("first titles",first_titles)
#print ("strings",strings)
#print ("urls",urls)
agg_count=0
for i in range(0, len(first_titles)):
titled=s.join(first_titles[i])
titled = titled.replace("'", "")
titled = titled.replace(" ", " ")
#print (titled)
string_compare=strings[i]+" "
string_compare=string_compare.replace("'", "")
string_compare=string_compare.replace(" ", " ")
#print (string_compare)
if (string_compare in titled) or (titled in string_compare):
#print (string_compare,titled)
agg_count=agg_count+1
agg_counts.append(agg_count)
#print ("ac",agg_counts)
for i in range(0, len(agg_counts)):
if agg_counts[i]>2:
agg_list.append(url)
if agg_counts[i]<=2:
not_agg_list.append(url)
print ("agg_list",agg_list)
print ("not_agg_list",not_agg_list)