async def parse_gold_info(page_content): soup = BeautifulSoup(page_content, 'lxml') table = soup.select('table.table-price').__getitem__(1) rows = table.find_all('tr') # print (last_updated) gold_date = str(rows[2].find_all('td')[0].text).strip() gold_24 = str(rows[2].find_all('td')[1].text).strip() gold_22 = str(rows[2].find_all('td')[3].text).strip() # print (gold_22 + " " + gold_24 + " " + gold_date) table = soup.select('table.table-price').__getitem__(3) rows = table.find_all("tr") silver_date = str(rows[1].find_all('td')[0].text).strip() silver = str(rows[1].find_all('td')[1].text).strip() # print (silver_date + " " + silver) last_updated = soup.find_all('p', class_='mob-cont')[0] last_updated = last_updated.text.strip() idx = util.index_of(last_updated, ":") if idx > 0: last_updated = last_updated[idx + 1:len(last_updated)] rate_info = RateInfo.RateInfo(gold_22, gold_24, silver, gold_date, last_updated) rate_info.set_error(None) LOGGER.info(str(rate_info)) return rate_info
async def parse_google_weather(page_content): soup = BeautifulSoup(page_content, 'lxml') seg_temp = soup.find_all('div', {'id': 'wob_wc'})[0] # print(seg_temp.text) # seg_temp = soup.find_all('div#wob_wc') # seg_temp = soup.find('div', class_='vk_c card-section') # print (seg_temp) span = seg_temp.select('span')[0] # First Segment element = span.select('div#wob_loc')[0] location = element.text as_of = '' # element = span.select('div#wob_dts')[0] # as_of = element.text element = span.select('span#wob_dc')[0] condition = element.text div_sub = seg_temp.select('div#wob_d')[0] # Second Segment element = div_sub.select('span#wob_tm')[0] temp = element.text div_sub = seg_temp.find_all('div', class_='vk_gy vk_sh')[1] # Second Sub element = div_sub.select('span#wob_hm')[0] humidity = element.text element = div_sub.select('span#wob_pp')[0] precipitation = element.text idx = util.index_of(precipitation, '%') if idx > 0: precipitation = precipitation + ' chance of rain until' # fetch low and high temperature for the day high = low = temp # div_forecast = seg_temp.find('div', class_='wob_df wob_ds') # Third Segment # div_sub = div_forecast.find('div', class_='vk_gy') # span = div_sub.select('span')[0] # high = span.text # # div_sub = div_forecast.find_all('div')[4] # span = div_sub.select('span')[0] # low = span.text weatherInfo = WeatherInfo.WeatherInfo(temp, low, high, as_of, condition, location, precipitation) weatherInfo.set_humidity(humidity) weatherInfo.set_error(None) LOGGER.info(str(weatherInfo)) return weatherInfo
def get_preciption(): line2 = '' if weather is not None: preciption = str(weather.get_preciption()) idx = util.index_of(preciption, '%') if idx > 0: preciption = preciption[0:idx+1] line2 = preciption return line2
def update_weather_location_line2(): global line2 location = weather.get_location() delimiter_idx = util.index_of(location, ',') if delimiter_idx > 0: location = location[0:delimiter_idx] # Make string 16 chars only and left justify with space if length is less. line2 = location[0:lcd_disp_length] line2 = line2.ljust(lcd_disp_length, ' ')
def update_weather_location_line2(): line2 = '' location = weather.get_location() delimiter_idx = util.index_of(location, ',') if delimiter_idx > 0: location = location[0:delimiter_idx] # Make string 16 chars only and left justify with space if length is less. line2 = location[0:DISPLAY_LENGTH] line2 = line2.ljust(DISPLAY_LENGTH, ' ') return line2
def update_weather_preciption_line2(): global line2 preciption = str(weather.get_preciption()) if len(preciption) > 0: idx = util.index_of(preciption, 'until') if idx > 0: preciption = preciption[0:idx] # Make string 20 chars only and left justify with space if length is less. line2 = preciption[0:lcd_disp_length] line2 = line2.ljust(lcd_disp_length, ' ') else: update_weather_location_line2()
def update_weather_updated(): line2 = '' preciption = str(weather.get_preciption()) if len(preciption) > 0: idx = util.index_of(preciption, 'until') if idx > 0: preciption = preciption[idx:len(preciption)] # Make string 20 chars only and left justify with space if length is less. line2 = preciption[0:DISPLAY_LENGTH] line2 = line2.ljust(DISPLAY_LENGTH, ' ') else: update_weather_location_line2() return line2
def update_weather_location_line2(): global line2 if weather is not None: location = weather.get_location() delimiter_idx = util.index_of(location, ',') if delimiter_idx > 0: location = location[0:delimiter_idx] # Make string 20 chars only and left justify with space if length is less. line2 = location[0:lcd_disp_length] # Make string right justified of length 4 by padding 3 spaces to left justl = lcd_disp_length - 4 location = location[0:justl] location = location.ljust(justl, ' ') line2 = location + ' ' + str(weather.get_temp()) + 'c'
async def parse_fuel_info(page_content): soup = BeautifulSoup(page_content, 'lxml') table = soup.select("table#BC_GridView1").__getitem__(0) # print (table) rows = table.find_all("tr") fuel_date = rows[1].find_all('td')[0].text petrol_price = str(rows[1].find_all('td')[1].text).strip() table = soup.select("table#BC_GridView1").__getitem__(1) # print (table) rows = table.find_all("tr") fuel_date = rows[1].find_all('td')[0].text diesel_price = str(rows[1].find_all('td')[1].text).strip() table = soup.find_all('table', {'id': 'tableb'}).__getitem__(1) # print (table) rows = table.find_all("tr") # print(rows) last_updated = rows[0].find_all('td', {'id': 'subcell'})[0] last_updated = last_updated.find_all('div')[3] # print (last_updated.text) last_updated = last_updated.text idx = util.index_of(last_updated, 'from ') if idx > 0: last_updated = last_updated[idx + 5:len(last_updated)] fuel_info = FuelInfo.FuelInfo(petrol_price, diesel_price, fuel_date, last_updated) fuel_info.set_error(None) LOGGER.info(str(fuel_info)) return fuel_info
async def parse_weather(page_content): soup = BeautifulSoup(page_content, 'lxml') # html.parser # lxml # html5lib seg_temp = soup.find_all('div', { 'id': 'WxuCurrentConditions-main-b3094163-ef75-4558-8d9a-e35e6b9b1034' })[0] # print(seg_temp.text) seg_temp = seg_temp.find_all('div', recursive=False)[0] seg_temp = seg_temp.find_all('section', recursive=False)[0] seg_temp = seg_temp.find_all('div', recursive=False)[0] div_ele = seg_temp.find_all('div', recursive=False)[0] # print(div_ele) location = div_ele.find('h1') location = location.text idx = util.index_of(location, ' Weather') if idx > 0: location = location[0:idx] as_of = div_ele.find('div') as_of = as_of.text idx = util.index_of(as_of, "as of ") idx_ist = util.index_of(as_of, " IST") if idx > 0: as_of = as_of[idx + 6:idx_ist] div_ele = seg_temp.find_all('div', recursive=False)[1] # print(div_ele) div_cond = div_ele.find_all('div', recursive=False)[0] # print(div_cond) element = div_cond.find('span') temp = element.text if len(temp) == 3: temp = temp[0:2] element = div_cond.find('div') condition = element.text div_cond = div_ele.find_all('div', recursive=False)[1] div_cond = div_cond.find_all('div', recursive=False)[0] element = div_cond.find_all('span')[0] high = element.text element = div_cond.find_all('span')[1] low = element.text if high == '--': high = temp if len(high) == 3: high = high[0:2] if len(low) == 3: low = low[0:2] preciption = seg_temp.find_all('div', recursive=False)[2] # print(preciption) if preciption is not None: preciption = preciption.text else: preciption = '' seg_temp = soup.find_all('div', {'id': 'todayDetails'}) # seg_temp = soup.find_all('div', {'id': 'WxuTodayDetails-main-fd88de85-7aa1-455f-832a-eacb037c140a'}) if seg_temp is not None and len(seg_temp) > 0: seg_temp = seg_temp[0] # print(seg_temp) div_ele = seg_temp.find('section') div_ele = div_ele.find_all('div', recursive=False)[1] div_node = div_ele.find_all('div', recursive=False)[2] # div_ele = div_node.find_all('div', recursive=False)[0] # print(div_ele.text) div_ele = div_node.find_all('div', recursive=False)[1] # print(div_ele) humidity = div_ele.text else: humidity = '0' weatherInfo = WeatherInfo.WeatherInfo(temp, low, high, as_of, condition, location, preciption) weatherInfo.set_humidity(humidity) weatherInfo.set_error(None) LOGGER.info(str(weatherInfo)) return weatherInfo
def remove_fraction(self, rate): idx = util.index_of(rate, ".") if idx > 0: rate = rate[0:idx] return rate