-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
44 lines (31 loc) · 880 Bytes
/
main.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
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
url = 'https://www.investing.com/indices/us-spx-500-historical-data'
result = requests.get(url)
doc = BeautifulSoup(result.text, 'lxml')
def allin(keywords, string):
for keyword in keywords:
if keyword not in string:
return False
return True
for table in doc.find_all("table"):
text = table.text.lower()
keywords = "date price open high low vol change".split()
if allin(keywords, text):
break
tbody = table.find("tbody")
dates = []
prices = []
for tr in tbody.find_all("tr"):
tds = tr.find_all("td")
date = tds[0].text
price = float(tds[1].text.replace(",",""))
dates.append(date)
prices.append(price)
print(dates, prices)
plt.plot(dates, prices)
plt.xticks(rotation=90)
plt.xlabel("date")
plt.ylabel("price")
plt.show()