-
Notifications
You must be signed in to change notification settings - Fork 129
/
hn_news_scraper_no_cred.py
86 lines (59 loc) · 2.07 KB
/
hn_news_scraper_no_cred.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
import requests # http requests
from bs4 import BeautifulSoup # web scraping
# Send the mail
import smtplib
# email body
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# system date and time manipulation
import datetime
now = datetime.datetime.now()
# email content placeholder
content = ''
#extracting Hacker News Stories
def extract_news(url):
print('Extracting Hacker News Stories...')
cnt = ''
cnt +=('<b>HN Top Stories:</b>\n'+'<br>'+'-'*50+'<br>')
response = requests.get(url)
content = response.content
soup = BeautifulSoup(content,'html.parser')
for i,tag in enumerate(soup.find_all('td',attrs={'class':'title','valign':''})):
cnt += ((str(i+1)+' :: '+ '<a href="' + tag.a.get('href') + '">' + tag.text + '</a>' + "\n" + '<br>') if tag.text!='More' else '')
#print(tag.prettify) #find_all('span',attrs={'class':'sitestr'}))
return(cnt)
cnt = extract_news('https://news.ycombinator.com/')
content += cnt
content += ('<br>------<br>')
content +=('<br><br>End of Message')
#lets send the email
print('Composing Email...')
# update your email details
# make sure to update the Google Low App Access settings before
SERVER = 'smtp.gmail.com' # "your smtp server"
PORT = 587 # your port number
FROM = '' # "your from email id"
TO = '' # "your to email ids" # can be a list
PASS = '*****' # "your email id's password"
# fp = open(file_name, 'rb')
# Create a text/plain message
# msg = MIMEText('')
msg = MIMEMultipart()
# msg.add_header('Content-Disposition', 'attachment', filename='empty.txt')
msg['Subject'] = 'Top News Stories HN [Automated Email]' + ' ' + str(now.day) + '-' + str(now.month) + '-' + str(
now.year)
msg['From'] = FROM
msg['To'] = TO
msg.attach(MIMEText(content, 'html'))
# fp.close()
print('Initiating Server...')
server = smtplib.SMTP(SERVER, PORT)
#server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
#server.ehlo
server.login(FROM, PASS)
server.sendmail(FROM, TO, msg.as_string())
print('Email Sent...')
server.quit()