-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
61 lines (48 loc) · 1.72 KB
/
scraper.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
#!/usr/bin/python3
# add local libraries
# mkdir lib
# pip3 install pandas bs4 -t lib/
# import local libraries
import sys
# change to your path - absolute path please
sys.path.append('/opt/spotscraper/lib')
# import scraper libs
import pandas as pd
import requests
from bs4 import BeautifulSoup
# import time lib
import datetime
# import json lib
import json
# get dates
today = datetime.date.today()
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
d1 = today.strftime("%Y-%m-%d")
d2 = tomorrow.strftime("%Y-%m-%d")
# set headers
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'}
# set epexspot URL
url='https://www.epexspot.com/en/market-data?market_area=BE&auction=MRC&trading_date=' + d1 + '&delivery_date=' + d2 + '&underlying_year=&modality=Auction&sub_modality=DayAhead&product=60&data_mode=table&period='
# get url
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, "html.parser")
# find the wanted table
table = soup.find('table', {"class": "table-01 table-length-1"})
# select data to filter
rows = table.find_all('tr')
data = []
for row in rows[6:]: #suppress first 6 lines
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
# print the data
result = pd.DataFrame(data, columns=['Buy Volume', 'Sell Volume', 'Volume', 'Price'])
result.reset_index(inplace=False)
result.drop('Buy Volume', inplace=True, axis=1)
result.drop('Sell Volume', inplace=True, axis=1)
result.drop('Volume', inplace=True, axis=1)
#print(result)
resultjson = result.to_json(orient="split")
parsed = json.loads(resultjson)
print(json.dumps(parsed, indent=4))