-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbursaIndicesPrice.py
53 lines (37 loc) · 1.4 KB
/
bursaIndicesPrice.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup as bs
from datetime import datetime
import pandas as pd
import time
url = 'https://www.bursamalaysia.com/market_information/indices_prices'
dfs = []
driver = webdriver.Chrome()
driver.get(url)
time.sleep(1)
#r = driver.page_source
for loop in range(1,3):
r = driver.page_source
soup = bs(r, "html.parser")
table = soup.find('div', {'class' : 'dataTables_scroll'})
a = table.find('table', {'id' : 'DataTables_Table_0'})
df = pd.read_html(str(a))[0]
dfs.append(df)
if loop == 2:
break
else:
button = driver.find_element_by_css_selector('[class="paginate_button page-item next"]')
ActionChains(driver).send_keys(Keys.CONTROL + Keys.HOME).perform()
ActionChains(driver).send_keys(Keys.PAGE_DOWN).perform()
time.sleep(1)
ActionChains(driver).send_keys(Keys.PAGE_DOWN).perform()
time.sleep(1)
ActionChains(driver).send_keys(Keys.PAGE_DOWN).perform()
#driver.execute_script("arguments[0].click()", button)
time.sleep(1)
button.click()
time.sleep(2)
b = pd.concat(dfs)
filename = datetime.now().strftime('bursaIndicesPrices_%Y%m%d.csv')
b.to_csv(filename, index=False)