-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGas_Price_Code.py
150 lines (125 loc) · 6.21 KB
/
Gas_Price_Code.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import requests
from bs4 import BeautifulSoup
import pandas as pd
#We can change fuel=A to B, C, D to see price for other type of gas
r = requests.get("http://www.californiagasprices.com/index.aspx?fuel=A")
rB = requests.get("http://www.californiagasprices.com/index.aspx?fuel=B")
rC = requests.get("http://www.californiagasprices.com/index.aspx?fuel=C")
rD = requests.get("http://www.californiagasprices.com/index.aspx?fuel=D")
soup = BeautifulSoup(r.content, "lxml")
soupB = BeautifulSoup(rB.content, "lxml")
soupC = BeautifulSoup(rC.content, "lxml")
soupD = BeautifulSoup(rD.content, "lxml")
table = soup.find("table", class_="p_v2")
table2 = soup.find("table", class_="p_v2 p_v2b")
tableB = soupB.find("table", class_="p_v2")
table2B = soupB.find("table", class_="p_v2 p_v2b")
tableC = soupC.find("table", class_="p_v2")
table2C = soupC.find("table", class_="p_v2 p_v2b")
tableD = soupD.find("table", class_="p_v2")
table2D = soupD.find("table", class_="p_v2 p_v2b")
#Regular Price
header = [th.get_text(strip=True) for th in table.tr.select("th")]
header2 = [th.get_text(strip=True) for th in table2.tr.select("th")]
#Midgrade
headerB = [th.get_text(strip=True) for th in tableB.tr.select("th")]
header2B = [th.get_text(strip=True) for th in table2B.tr.select("th")]
#Premium
headerC = [th.get_text(strip=True) for th in tableC.tr.select("th")]
header2C = [th.get_text(strip=True) for th in table2C.tr.select("th")]
#Diesel Fuel
headerD = [th.get_text(strip=True) for th in tableD.tr.select("th")]
header2D = [th.get_text(strip=True) for th in table2D.tr.select("th")]
all_data = []
all_dataB = []
all_dataC = []
all_dataD = []
for row in table.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_data.append([price, station, area, thanks])
for row in table2.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_data.append([price, station, area, thanks])
for row in tableB.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_dataB.append([price, station, area, thanks])
for row in table2B.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_dataB.append([price, station, area, thanks])
for row in tableC.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_dataC.append([price, station, area, thanks])
for row in table2C.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_dataC.append([price, station, area, thanks])
for row in tableD.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_dataD.append([price, station, area, thanks])
for row in table2D.select("tbody tr:has(th)"):
price = row.select_one("th div.price_num").text
address = row.select_one("td dl.address")
station = (address.dt.text.strip(), address.dd.text)
area = row.select_one("td a.p_area").text
thanks_by = row.select_one("td a.mem").text
last_updated_thanks = row.select_one("td div.tm").text
thanks = (thanks_by, last_updated_thanks)
all_dataD.append([price, station, area, thanks])
df = pd.DataFrame(all_data, columns=header)
df.insert(0, 'last_updated_price', pd.to_datetime('now').replace(microsecond=0))
#print(df)
dfB = pd.DataFrame(all_dataB, columns=header)
dfB.insert(0, 'last_updated_price', pd.to_datetime('now').replace(microsecond=0))
#print(dfB)
dfC = pd.DataFrame(all_dataC, columns=header)
dfC.insert(0, 'last_updated_price', pd.to_datetime('now').replace(microsecond=0))
#print(dfC)
dfD = pd.DataFrame(all_dataD, columns=header)
dfD.insert(0, 'last_updated_price', pd.to_datetime('now').replace(microsecond=0))
#print(dfD)
#print(dfD.dtypes)
df.to_csv("/Users/annanguyen/Documents/Anna_Projects/Web_Scraping_Python_projects/Regular_Gas.csv", mode='a', index=False)
dfB.to_csv("/Users/annanguyen/Documents/Anna_Projects/Web_Scraping_Python_projects/Midgrade_Gas.csv", mode='a', index=False)
dfC.to_csv("/Users/annanguyen/Documents/Anna_Projects/Web_Scraping_Python_projects/Premium_Gas.csv", mode='a', index=False)
dfD.to_csv("/Users/annanguyen/Documents/Anna_Projects/Web_Scraping_Python_projects/Diesel_Fuel_Gas.csv", mode='a', index=False)