-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_prices.py
59 lines (45 loc) · 1.98 KB
/
get_prices.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
import argparse, datetime
import pandas as pd
from time import sleep
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
# parsing and configuration
def parse_args():
desc = "Scrape historical prices from marketsinsider"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--query', type=str)
return parser.parse_args()
args = parse_args()
query = args.query
def get_prices(query, query_class, start_date, end_date):
markets_url = f'https://markets.businessinsider.com/{query_class}/historical-prices'
if(query_class=='index'):
query_url = f'{markets_url}/{query}/{start_date}_{end_date}'
else:
query_url = f'{markets_url}/{query}/usd/{start_date}_{end_date}'
print(f'Accessing Webpage for {query} from {start_date} to {end_date}')
# need geckodriver installed for this to run
browser = webdriver.Firefox(executable_path='C:/Users/Steph/bin/geckodriver.exe')
browser.get(query_url)
#search_form = browser.find_element_by_id('historic-price-list')
# allow sufficient time for browser to load
sleep(10)
test = browser.find_element_by_xpath('//*[@id="historic-price-list"]/div/div[2]/table')
#test = browser.find_element_by_xpath('//html/body/main/div/div/div[3]/div/div[10]/div/div[2]/table')
#test = browser.find_element_by_css_selector('table.table.instruments')
soup = BeautifulSoup(test.get_attribute("outerHTML"), 'lxml')
dates = []
close_prices = []
for tr in soup.find_all('tr')[2:]:
tds = tr.find_all('td')
dates.append(tds[0].text.strip())
close_prices.append(tds[1].text.strip())
browser.close()
browser.quit()
price_df = pd.DataFrame({
'Date':dates, 'Close':close_prices,
'Query_class':query_class, 'Query':query})
#price_df.to_csv(f'data/{query}_prices.csv', index=False)
print(f'Prices Obtained for {query} from {start_date} to {end_date}')
return(price_df)