Jan-24-2019, 03:47 PM
Thanks for your reply Larz60+, as requested here is the entire code. It is only my "if" statement that isn't working like I thought.
#---- National Weather System by Python: from user_agent import generate_user_agent from bs4 import BeautifulSoup import requests headers = {'User-Agent': generate_user_agent(device_type='desktop', os=('mac', 'linux'))} page_response = requests.get('https://forecast.weather.gov/MapClick.php?lat=32.47318000000007&lon=-100.40478999999999#.XC2EbM1MFEa/', timeout=5, headers=headers) soup = BeautifulSoup(page_response.content, 'html.parser') current = soup.find(id='current_conditions-summary') forecast = current.find_all(class_='myforecast-current') condition = forecast[0] weather = [] period = current.find(class_='myforecast-current').text temp_f = current.find(class_='myforecast-current-lrg').text temp_c = current.find(class_='myforecast-current-sm').text weather.append(period) weather.append(temp_f) weather.append(temp_c) table = soup.find('table') for row in table.find_all('tr'): key = (' '.join(td.text.strip() for td in row.find_all('td'))) weather.append(key) seven_day = soup.find(id='seven-day-forecast') forecast_items = seven_day.find_all(class_='tombstone-container') current = forecast_items[0] time = current.find(class_='period-name').text short_desc = current.find(class_='short-desc').text temp = current.find(class_='temp temp-()') if temp == ('low'): find(class_='temp temp-low').text if temp == ('high'): find(class_='temp temp-high').text weather.append(time) weather.append(short_desc) weather.append(temp) with open('current-weather.dat', 'w') as f: for info in weather: f.write(f'{info}\n')