-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniversity_news_scrapping.py
151 lines (80 loc) · 2.65 KB
/
university_news_scrapping.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
""" Import webdriver from selenium """
from selenium import webdriver
import csv
import schedule
import time
from datetime import date
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
""" Import BeautifulSoup from bs4 """
from bs4 import BeautifulSoup
def daily_news():
""" Load chrome webdriver """
driver = webdriver.Chrome(executable_path = '/Users/akhilreddy/Downloads/chromedriver')
""" Get the URL of page to be loaded """
url = 'http://www.sathyabama.ac.in'
""" Download the html page """
driver.get(url)
""" To retrieve the source of page loaded using webdriver """
html_doc = driver.page_source
""" Passing html_doc to BeautifulSoup to parse """
soup = BeautifulSoup(html_doc, 'lxml')
""" Organize html source """
soup.prettify()
""" Select by class """
today = str(date.today())
data_list = []
class_select_data = soup.find('ul', class_ = 'latest_news')
li_select_data = class_select_data.find_all('a')
for list_data in li_select_data:
data=[list_data.text, list_data['href']]
data_list.append(data)
with open ('/' + today + '.csv','w') as file:
writer = csv.writer(file)
for row in data_list:
writer.writerow(row)
fromaddr = "[email protected]"
toaddr = "[email protected]"
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddr
# storing the receivers email address
msg['To'] = toaddr
# storing the subject
msg['Subject'] = "Subject of the Mail"
# string to store the body of the mail
body = "Today's News published"
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = today + '.csv'
attachment = open('news/' + today + '.csv', 'rb')
# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(fromaddr, "AkHiL777")
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(fromaddr, toaddr, text)
# terminating the session
s.quit()
schedule.every().day.at("23:17").do(daily_news)
while True:
schedule.run_pending()
time.sleep(1)