Skip to content

Instantly share code, notes, and snippets.

@shekharkoirala
Created June 1, 2019 07:19
Show Gist options
  • Save shekharkoirala/8e63ca45b0ded51ac00fab3b00db56e0 to your computer and use it in GitHub Desktop.
Save shekharkoirala/8e63ca45b0ded51ac00fab3b00db56e0 to your computer and use it in GitHub Desktop.
Scrap Nepali share market and send email to anyone. The message will be top 5 share difference ( rise ).
import requests
from bs4 import BeautifulSoup
def parse_df(link, columns):
page = requests.get(link)
soup = BeautifulSoup(page.content, 'html.parser')
temp_data = []
final_data = []
data2 = soup.find_all(class_='container')
data_ = data2[4]
tag_type = type(data_.contents[3].contents[1])
string_type = type(data_.contents[3].contents[0])
for d_ in data_.contents[3].contents:
if isinstance(d_, tag_type):
if len(d_.contents) > 10:
for inside_d in d_.contents:
if isinstance(inside_d, tag_type):
if (isinstance(inside_d.contents[0], string_type)) and (str(inside_d.contents[0]) != "S.N."):
temp_data.append(str(inside_d.contents[0]).split(".")[0])
final_data.append(temp_data)
temp_data = []
df = pd.DataFrame(final_data,columns= columns)
df.dropna(inplace=True)
return df
def parse_column_name(link):
page = requests.get(link)
soup = BeautifulSoup(page.content, 'html.parser')
data = soup.find_all(class_='alnright')
columns = [_.find('a').contents[0] for _ in data if _.find('a')]
columns.insert(0,'Traded Companies')
columns.insert(0, 'S.N.')
return columns
def scrap_data():
link = "http://nepalstock.com/main/todays_price/index/1/"
column = parse_column_name(link)
df = pd.DataFrame(columns= column)
for num_ in range(1, 10):
link = "http://nepalstock.com/main/todays_price/index/"+ str(num_) +"/"
df_ = parse_df(link,column)
df = pd.concat([df,df_],ignore_index=True)
return df
import time
start = time.time()
df = scrap_data()
end = time.time()
print(end- start)
#for sorting we have to change the dtyoe of pandas object to numeric value.
df["Difference Rs."] = pd.to_numeric(df["Difference Rs."])
#create a message for email
message = df.sort_values(['Difference Rs.'], ascending=False).head(5).to_json()
#send email to anyone.
import smtplib, ssl
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "" # Enter your address
receiver_email = "" # Enter receiver address
cc_email = "" #Enter cc address
bcc_email = "" #Enter bcc address
password = "" # password of sender email
message_subject = "" # Enter subject
message_text = message # right now json of top 5 share changes positively.
def send_last_email():
message = "From: %s\r\n" % sender_email + "To: %s\r\n" % receiver_email + \
"CC: %s\r\n" % cc_email + "BCC: %s\r\n" % bcc_email \
+ "Subject: " "%s\r\n" % message_subject + "\r\n" + message_text
receiver_emails = [receiver_email] + [cc_email] + [bcc_email]
context = ssl.create_default_context()
server = smtplib.SMTP_SSL(smtp_server, port, context=context)
server.login(sender_email, password)
server.set_debuglevel(1)
server.sendmail(sender_email, receiver_emails, message)
server.quit()
send_last_email()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment