Last active
February 9, 2017 08:51
-
-
Save phondanai/9c0d0ae454361e268fd2a8ad3010c983 to your computer and use it in GitHub Desktop.
Get fuel price from Bangchak web widget, แสดงราคาน้ำมัน บางจาก
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
import requests | |
URL = "http://www.bangchak.co.th/oilprice-widget.aspx" | |
FUEL_TYPE = ["Hi-Diesel", | |
"Gasohol E85", | |
"Gasohol E20", | |
"Gasohol 91", | |
"Gasohol 95", | |
"NGV"] | |
def compare_price(today, tomorrow): | |
if today < tomorrow: | |
return " ↑↑\n" | |
elif today > tomorrow: | |
return " ↓↓\n" | |
else: | |
return " ==\n" | |
def get_oil_price_bangchak(): | |
"""Return string of fuel price list from Bangchak""" | |
html_content = requests.get(URL).text | |
soup = BeautifulSoup(html_content, 'lxml') | |
today = soup.find("td", {"class": "css1"}).contents[2] | |
today_price_list = soup.find_all("td", {"class": "css2"}) | |
tomorrow_price_list = soup.find_all("td", {"class": "css3"}) | |
today_ngv_price = today_price_list[-1].contents[0] | |
tomorrow_ngv_price = tomorrow_price_list[-1].contents[0] | |
result = """ราคาน้ำมัน ณ วันที่ {}\n | |
ประเภทน้ำมัน\t\t\t\tวันนี้\t\tพรุ่งนี้\n | |
\t\t\t\t\tบาท/ลิตร\t\tบาท/ลิตร\n""".format(today) | |
for fuel, today_price, tomorrow_price in zip(FUEL_TYPE[:-1], | |
today_price_list[:5], | |
tomorrow_price_list[:5]): | |
result += fuel+"\t\t\t\t"+today_price.contents[0]+"\t\t"+tomorrow_price.contents[0] | |
result += compare_price(float(today_price.contents[0]), float(tomorrow_price.contents[0])) | |
result += "\n\t\t\t\t\tบาท/กก.\t\tบาท/กก.\n" | |
result += FUEL_TYPE[-1]+"\t\t\t\t\t"+today_ngv_price+"\t\t"+tomorrow_ngv_price | |
result += compare_price(float(today_ngv_price), float(tomorrow_ngv_price)) | |
return(result) | |
print(get_oil_price_bangchak()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment