-
Notifications
You must be signed in to change notification settings - Fork 6
/
portalgov.py
147 lines (125 loc) · 3.85 KB
/
portalgov.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
# - * -coding: utf - 8 - * -
# encoding = utf8
import sys
from importlib import reload
reload(sys)
sys.setdefaultencoding('utf8')
import json
import time
import difflib
from datetime import date, timedelta
from bs4 import BeautifulSoup
import requests
import os
from datetime import date, timedelta
import time
link_set = set()
visited = set()
count = 0
def loadLinks():
global link_set
global visited
with open('all.txt', 'r') as file1:
with open('visited.txt', 'r') as file2:
visited = set(file2)
link_set = set(file1).difference(file2)
def extract_data(article_soup, output_file_name):
try:
article_content = ""
paragraphs = article_soup.find_all("p", {"style": "text-align:justify"})
listitems = article_soup.find_all("li", {"style": "text-align:justify"})
for para in paragraphs:
article_content += para.get_text().strip() + "\n"
for li in listitems:
article_content += li.get_text().strip() + "\n"
except:
article_content = ""
data = "<article>\n"
data += "<text>\n" + article_content + "\n</text>\n"
data += "</article>"
output_dir = './Data/'
raw_output_dir = './' + "Raw" + '/'
try:
os.makedirs(output_dir)
except OSError:
pass
try:
os.makedirs(raw_output_dir)
except OSError:
pass
try:
with open(raw_output_dir + '/' + str(output_file_name), 'w') as file:
file.write(str(article_soup))
except Exception as e:
print(e)
pass
if len(paragraphs) == 0 and len(listitems) == 0:
return
try:
with open(output_dir + '/' + str(output_file_name), 'w') as file:
file.write(data)
except Exception as e:
print(e)
pass
def start():
base_url = 'http://dae.gov.bd'
try:
print(base_url)
article_soup = requests.get(base_url)
soup = BeautifulSoup(article_soup.content, "html.parser")
visited.add(base_url)
with open("visited.txt", "a") as visited_file:
visited_file.write(base_url + "\n")
except Exception as e:
print(e)
exit()
all_links = soup.find_all("a")
if len(all_links) == 0:
exit()
else:
for link in all_links:
lnk = link.get('href')
if lnk.startswith("/site"):
lnk = "http://dae.portal.gov.bd" + lnk
elif "http://dae.portal.gov.bd" in lnk:
pass
else:
continue
link_set.add(lnk)
with open("all.txt", "a") as all_file:
all_file.write(lnk + "\n")
restart()
def restart():
global link_set
while len(link_set) > 0:
global count
url = link_set.pop()
if url.startswith("/site"):
url = "http://dae.portal.gov.bd" + url
elif "http://dae.portal.gov.bd" in url:
pass
else:
continue
visited.add(url)
with open("visited.txt", "a") as visited_file:
visited_file.write(url + "\n")
soup = requests.get(url)
article_soup = BeautifulSoup(soup.content, "html.parser")
print(url)
extract_data(article_soup, count)
count = count + 1
new_links = article_soup.find_all("a")
if len(new_links) == 0:
pass
else:
for full_link in new_links:
link = full_link.get('href')
if link in visited:
continue
link_set.add(link)
with open("all.txt", "a") as all_file:
all_file.write(link + "\n")
if __name__ == '__main__':
# start()
loadLinks()
restart()