-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (55 loc) · 2.07 KB
/
app.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
from flask import Flask,jsonify
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
@app.route('/')
def index():
return "Thanks for checking out my Stock API! Instructions are in the documentation here: "
@app.route('/<ticker>')
def getStock(ticker):
try:
baseUrl = 'https://www.marketwatch.com/investing/fund/'
url = baseUrl + ticker
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
price = soup.find("meta", {"name": "price"})['content']
change = soup.find("meta", {"name": "priceChange"})['content']
percent = soup.find("meta", {"name": "priceChangePercent"})['content']
return jsonify({'price': price, '$ Change': change, 'percent': percent})
except:
return "Ticker not Found"
@app.route('/<ticker>/price')
def stockPrice(ticker):
try:
baseUrl = 'https://www.marketwatch.com/investing/fund/'
url = baseUrl + ticker
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
price = soup.find("meta", {"name": "price"})['content']
return jsonify({'price': price})
except:
return "Ticker not Found"
@app.route('/<ticker>/change')
def stockChange(ticker):
try:
baseUrl = 'https://www.marketwatch.com/investing/fund/'
url = baseUrl + ticker
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
change = soup.find("meta", {"name": "priceChange"})['content']
return jsonify({'$ Change': change})
except:
return "Ticker not Found"
@app.route('/<ticker>/percent')
def stockPercent(ticker):
try:
baseUrl = 'https://www.marketwatch.com/investing/fund/'
url = baseUrl + ticker
result = requests.get(url)
soup = BeautifulSoup(result.text, "html.parser")
percent = soup.find("meta", {"name": "priceChangePercent"})['content']
return jsonify({'Percent Change': percent})
except:
return "Ticker not Found"
if __name__ == "__main__":
app.run(debug=True)