Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
|
4 |
+
def get_stock_data(symbol):
|
5 |
+
url = f"https://finance.yahoo.com/quote/{symbol}"
|
6 |
+
response = requests.get(url)
|
7 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
8 |
+
|
9 |
+
data = {}
|
10 |
+
data['symbol'] = symbol
|
11 |
+
data['price'] = soup.find_all('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find('span').text
|
12 |
+
data['change'] = soup.find_all('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})[0].find_all('span')[1].text
|
13 |
+
|
14 |
+
return data
|
15 |
+
|
16 |
+
def get_news(symbol):
|
17 |
+
url = f"https://finance.yahoo.com/quote/{symbol}/news?p={symbol}"
|
18 |
+
response = requests.get(url)
|
19 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
20 |
+
|
21 |
+
news = []
|
22 |
+
for item in soup.find_all('h3', {'class': 'Mb(5px)'}):
|
23 |
+
news_item = {}
|
24 |
+
news_item['title'] = item.text
|
25 |
+
news_item['link'] = item.find('a')['href']
|
26 |
+
news.append(news_item)
|
27 |
+
|
28 |
+
return news
|
29 |
+
|
30 |
+
symbol = input("Please enter a stock symbol: ")
|
31 |
+
print(get_stock_data(symbol))
|
32 |
+
print(get_news(symbol))
|