forked from KSMubasshir/bd-newspaper-crawlers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcadetcollegeblog.py
148 lines (119 loc) · 4.64 KB
/
cadetcollegeblog.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
import os
import json
import time
from datetime import date, timedelta
from bs4 import BeautifulSoup
import requests
import time
from stem import Signal
from stem.control import Controller
import shutil
from random import uniform
from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
# from io import open
def renew_tor_ip():
with Controller.from_port(port=9051) as controller:
controller.authenticate(password="abhik")
controller.signal(Signal.NEWNYM)
def getData(link, session=None):
global TOR_SWITCH
if not session:
session = webdriver.Firefox()
time.sleep(9)
while True:
try:
session.get(link)
except Exception as e:
print(e)
renew_tor_ip()
session.quit()
profile = webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
profile.set_preference('permissions.default.image', 2)
profile.set_preference('browser.link.open_newwindow', 1)
session = webdriver.Firefox(profile)
print("********Changing IP************")
time.sleep(15)
continue
else:
return session
newspaper_base_url = 'https://cadetcollegeblog.com/'
for index in range(1, 577):
url = newspaper_base_url + "page/" + str(index)
try:
print(url)
archive_soup = requests.get(url)
except:
print("No response for links in archive,passing")
continue
soup = BeautifulSoup(archive_soup.content, "html.parser")
try:
with open("test.txt", 'w', encoding='utf8') as file:
file.write(str(soup))
except:
pass
exit()
all_links = soup.find_all("a", attrs={"class": "title"})
page_links_length = len(all_links)
if (page_links_length == 0):
break
else:
for link in all_links:
link_separator = link.get('href')
link = "https://www.kalerkantho.com" + link_separator[1:]
article_url = link
link_tokens = link_separator.split("/")
year = link_tokens[3]
month = link_tokens[4]
day = link_tokens[5]
output_file_name = link_tokens[2] + "_" + link_tokens[3] + "_" + link_tokens[4] + "_" + link_tokens[
5] + "_" + link_tokens[6]
output_dir = './{}/{}/{}/bn'.format(year, month, day)
raw_output_dir = '../' + "Raw" + '/' + "Kalerkantho" + '/' + output_dir
try:
os.makedirs(output_dir)
except OSError:
pass
try:
os.makedirs(raw_output_dir)
except OSError:
pass
try:
session = getData(article_url, session)
except:
print("No response for content in link,trying to reconnect")
time.sleep(2)
continue
article_soup = BeautifulSoup(session.page_source, "html.parser")
print(article_url)
paragraphs = article_soup.find_all("p")
length = len(paragraphs)
i = 0
article_content = ""
for paragraph in paragraphs:
if i == 0:
date = paragraph.get_text().split("|")[2]
elif i > 3 and i <= length - 2:
article_content += paragraph.get_text() + "\n"
else:
pass
i = i + 1
data = "<article>\n"
# data += "<title>" + title + "</title>\n"
data += "<date>" + date + "</date>\n"
# data += "<author>" + author + "</author>\n"
data += "<text>" + article_content + "</text>\n"
data += "</article>"
with open(output_dir + '/' + output_file_name, 'w', encoding='utf8') as file:
file.write(data)