Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still learning, class var?
#3
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')
Reply


Messages In This Thread
Still learning, class var? - by b4iknew - Jan-24-2019, 03:26 PM
RE: Still learning, class var? - by Larz60+ - Jan-24-2019, 03:36 PM
RE: Still learning, class var? - by b4iknew - Jan-24-2019, 03:47 PM
RE: Still learning, class var? - by Larz60+ - Jan-24-2019, 03:57 PM
RE: Still learning, class var? - by Larz60+ - Jan-24-2019, 05:15 PM
RE: Still learning, class var? - by snippsat - Jan-24-2019, 06:01 PM
RE: Still learning, class var? - by b4iknew - Jan-24-2019, 08:33 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020