-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergyData.py
72 lines (57 loc) · 2.72 KB
/
energyData.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
65
66
67
68
69
70
71
72
#Simple Script to calculate the gas savings of a Tesla using average gas prices
#in big cities around the country
import bs4 as bs
import requests
import pandas as pd
import numpy as np
class energyData(object):
zip_code = '78666'#input("Enter Zip Code: ")
miles_Driven = '700'#input("Miles driven per month: ")
user_state = ""
gas_avg = ""
gas_low = ""
state_elec_rate = ""
def __init__(self):
return
def get_data(self):
#take the users input as the zip_code to search for on gasbuddy
data = pd.read_csv('electric_rates.csv')
states = data.iloc[3:53, 0:2].values
elec_data = dict((x[0], (x[1])) for x in states[1:])
#allows access to the url
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
gas_url= "https://www.gasbuddy.com/home?search=" + self.zip_code + "&fuel=1"
#grabs all html code from url
html_code = requests.get(gas_url, headers=headers)
#stores the code in a beautifulSoup object to allow for parsing
price_soup = bs.BeautifulSoup(html_code.text, 'html.parser')
#looks for specific html code to find average prices, varies depending on code you want to parse
gas_results = price_soup.find_all('', attrs={'class':'style__header3___3T2tm style__header___onURp style__snug___2HJ4K styles__priceHeader___38ONR'})
state_results = price_soup.find_all('', attrs={'class':'styles__resultsHeader___1TEzC'})
state = str(state_results[0].contents)
#state = str(state.contents)
state = state[2:]
STATE = []
if ', ' in state:
start_index = state.index(",")
end_index = state.index("'")
for x in range(start_index+2, end_index):
STATE.append(state[x])
if 'n ' in state:
start_index = state.index("n")
end_index = state.index("'")
for x in range(start_index+2, end_index):
STATE.append(state[x])
self.user_state = "".join(STATE)
#store the html code we want into a string variable so we can grab the average price
lowest_price = gas_results[0]
average_price = gas_results[1]
self.gas_avg = str(average_price.contents)
self.gas_avg = self.gas_avg[3:7]
self.gas_low = str(lowest_price.contents)
self.gas_low = self.gas_low[3:7]
#takes the state we are in as output, compares with dictionary of electric rates to find states rate
for x in range(len(elec_data)):
if self.user_state in elec_data:
self.state_elec_rate = elec_data[self.user_state]
break