-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahoo_fin_helper.py
55 lines (35 loc) · 1.73 KB
/
yahoo_fin_helper.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
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 6 01:43:43 2020
"""
from bs4 import BeautifulSoup
import requests
simple_headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"}
def BNprice():
url = 'https://finance.yahoo.com/quote/^NSEBANK?p=^NSEBANK&.tsrc=fin-srch'
try:
response = requests.get(url, headers=simple_headers )
except:
print("issue fetching bank nifty")
soup = BeautifulSoup(response.text, 'lxml')
index = soup.find_all('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find('span').text
index = index.replace(',','')
changes = soup.find_all('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find_all('span')[1].text
abs_index_change = changes.split( "(" )[0];
pc_index_change = changes.split( "(" )[1];
pc_index_change = pc_index_change.split( "%)" )[0];
return( index, abs_index_change, pc_index_change)
def Niftyprice():
url = 'https://finance.yahoo.com/quote/^NSEI?p=^NSEI&.tsrc=fin-srch'
try:
response = requests.get(url, headers=simple_headers )
except:
print("issue fetching nifty")
soup = BeautifulSoup(response.text, 'lxml')
index = soup.find_all('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find('span').text
index = index.replace(',','')
changes = soup.find_all('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find_all('span')[1].text
abs_index_change = changes.split( "(" )[0];
pc_index_change = changes.split( "(" )[1];
pc_index_change = pc_index_change.split( "%)" )[0];
return( index, abs_index_change, pc_index_change )