-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.py
328 lines (237 loc) · 10.8 KB
/
test4.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
def error(s):
if str(s)=="None" or len(s)==0:
return 1
else:
return 0
def clean(s):
s=s.strip()
s=s.replace("\n"," ")
s=s.replace(","," ")
return s
dontscrap=0
test=0
test_limit=10
cnt=0
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as bsoup
my_url='https://forum.ge/?s=989cb85ec94736224d89a77e47c5d274&act=idx'
print("Provide the main link")
#my_url=input()
print("Enter Table N.O , 0 or 1 or 2")
tablenum=input()
tablenum=int(tablenum)
uClient=uReq(my_url)
page_html = uClient.read()
uClient.close()
soup = bsoup(page_html,"html.parser")
tables=soup.findAll("div",{"class","tableborder"})
link_f=open("C://Users//Jai//Desktop//links"+str(tablenum)+".csv","w",encoding='utf-8')
link_f.write("links scraped\n")
file=open("C://Users//Jai//Desktop//scrap"+str(tablenum)+".csv","w",encoding='utf-8')
headers="link_name,Topicname,thread_author,views,post_author,date,posttext\n"
file.write(headers)
my_cnt=-1
links_set=set()
print("NUMBER OF TABLES",len(tables))
for tt in tables:
my_cnt+=1
if my_cnt!=tablenum:
continue
mini_tables=tables[tablenum].findAll("td",{"class":"row4"})
if my_cnt>3:
break
for table in mini_tables:
links=table.find_all("a",href=True)
for j in links:
try:
link_name=j.text.strip()
link_name=clean(link_name)
base_url=j['href']
print("link_name=",link_name,"BASE_URL=",base_url)
if dontscrap:
continue
uClient=uReq(base_url)
page_html = uClient.read()
uClient.close()
soup = bsoup(page_html,"html.parser")
##################################
headings=soup.findAll("td",{"nowrap":"nowrap"})
# print(headings[len(headings)-1].text.strip()) # gives number of pages after including this page
# headings = Pages: (24) [1] 2 3 ... Last » ( Go to first unread post )
nn=0
if str(headings)!="None" and len(headings)>0:
s=headings[len(headings)-1].text.strip()
try:
s=s[s.find("(")+1:s.find(")")]
#print(s)
nn=int(s) # number of pages to be scraped i=1 to n-1 add i*15 to url
except:
pass
print("NUMBER OF PAGES WITH LINKS:",nn,"\n")
#######################################
for i in range(nn):
try:
# PAGE LINK TRAVERSAL
print("SCRAPING PAGE WITH POST LINKS : ",i+1)
new_url=base_url+"&st="+str(i*40)
uClient=uReq(base_url+"&st="+str(i*40))
page_html = uClient.read()
uClient.close()
soup = bsoup(page_html,"html.parser")
###################################
# get main title
main="None"
main=soup.find("div",{"class":"maintitle"})
if main!="None":
main=main.text.strip()
maintopic=str(main)
tables=soup.findAll("div",{"class":"tableborder"})
rows=""
for table in tables:
mt=table.find("div",{"class":"maintitle"})
if str(mt)!="None":
# get all rows with the links
rows=table.findAll("tr")
print("TOTAL ROWS=",len(rows))
# pick a row of topic
count=0
for row in rows:
if count==0: # first link got taken twice hence added this
count+=1
continue
thread_author=""
views=""
link=""
topicname=""
#row=row[8]
if row!="None" and len(row)>0:
linkboxes=row.findAll("td",{"class":"row4"})
linkboxdata=[]
if error(linkboxes):
#print("\nERROR\n")
continue
for i in linkboxes:
linkboxdata.append(i.text.strip())
# topicname
topicname=linkboxdata[1].split("(")[0]
##############################
link=""
links=row.find_all("a",href=True,title=True)
if str(links)=="None" or len(links)==0:
continue
link=links[0]['href']
######################################
if len(linkboxdata)>=5:
# # topicname
# topicname=linkboxdata[1].split("(")[0]
print("D")
# Thread author
thread_author=linkboxdata[2]
# views
views=linkboxdata[4]
# print("topicname:",topicname," thread_author",thread_author," views",views)
# print("\n")
else:
print("L")
linkboxes2=row.findAll("td",{"class":"row2"})
if error(linkboxes2):
#print("\nERROR\n")
continue
thread_author=linkboxes2[1].text.strip()
views=linkboxes2[2].text.strip()
# print(maintopic)
topicname=topicname.strip()
thread_author=thread_author.strip()
topicname=clean(topicname)
thread_author=clean(thread_author)
views=clean(views)
cnt+=1
if test and cnt>test_limit:
break
print("topicname:",topicname," thread_author",thread_author," views",views,"\nlink",link,"\n")
# NOW THE LINK NEED TO BE SCRAPED
my_url=link
if link in links_set:
print("DUPLICATE LINK")
continue
print("WRITING TO LINK_F")
links_set.add(link)
link_f.write(link+"\n")
uClient=uReq(my_url)
page_html = uClient.read()
uClient.close()
soup = bsoup(page_html,"html.parser")
#posts=soup.findAll("div",{"class":"postcolor"})
###############################
headings=soup.findAll("td",{"nowrap":"nowrap"})
# print(headings[len(headings)-1].text.strip()) # gives number of pages after including this page
# headings = Pages: (24) [1] 2 3 ... Last » ( Go to first unread post )
n=0
if str(headings)!="None" and len(headings)>0:
s=headings[len(headings)-1].text.strip()
try:
s=s[s.find("(")+1:s.find(")")]
#print(s)
n=int(s) # number of pages to be scraped i=1 to n-1 add i*15 to url
except:
pass
print("NUMBER OF PAGES:",n,"\n")
for i in range(n):
try:
print("SCRAPING PAGE : ",i+1)
uClient=uReq(my_url+"&st="+str(i*15))
page_html = uClient.read()
uClient.close()
soup = bsoup(page_html,"html.parser")
#############
tables=soup.findAll("div",{"class":"tableborder"})
for table in tables:
# table=tables[0]
if len(table)>0:
boxes=table.findAll("table")
if str(boxes)!="None" and len(boxes)>0:
for box in boxes:
# box=boxes[1]
username=box.find("span",{"class":"normalname"})
if str(username)=="None":
continue
username=username.text.strip()
posttexts=box.findAll("div",{"class":"postcolor"})
if str(posttexts)=="None" or len(posttexts)==0:
continue
posttext=""
for pt in posttexts:
posttext+=pt.text.strip()
# date
textboxes=box.findAll("td")
if str(textboxes)=="None" or len(textboxes)<=1:
continue
date=(textboxes[1]).text.strip().split(":")[1].split(",")[0]
quote=""
quotes=box.findAll("td",{"id":"QUOTE"})
if str(quotes)=="None" or len(quotes)==0:
lol=1
else:
for q in quotes:
p=q.text.strip()
posttext+=" "+p
quote+=" "+p
print("\nusername:",username,"\nposttext:",posttext,"\ndate:",date,"\nquote",quote,"\n")
u=username.replace(","," ")
u=u.replace("\n"," ")
d=date.replace(","," ")
d=d.replace("\n"," ")
p=posttext.replace(","," ")
p=p.replace("\n"," ")
file.write(link_name+","+topicname+","+thread_author+","+views+","+u+","+d+","+p+"\n")
except:
#pass
continue
except:
continue
except:
print("exception with main page link")
pass
print("CLOSING FILE")
file.close()
link_f.close()