Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting started
#1
Hello and good morning all.
I want to make it clear, I am not a programmer by any streach of the imagination. I just started trying to learn python a few days ago. My code works fine, but, it seems highly inefficient to me. As you can see I have opened and closed the same file 3 times to write too, and append my text file. I thought I could figure out a way to open the file just one time and write all the same data too it but I have failed.

Any thoughts or suggestions would be highly appreciated.
Have a great day.

#----  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'))}
# headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686 on x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.63 Safari/537.36'}
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.findAll(class_='myforecast-current')
condition = forecast[0]

period = current.find(class_='myforecast-current').get_text()
temp_f = current.find(class_='myforecast-current-lrg').get_text()
temp_c = current.find(class_='myforecast-current-sm').get_text()

with open('C:\\Users\\user\\Documents\\Weather\\current-weather.txt', 'w') as f:
	print(period + '\n', temp_f + '\n', temp_c, file=f)

table = soup.find('table')
with open('C:\\Users\\user\\Documents\\Weather\\current-weather.txt', 'a') as f:
    for row in table.findAll('tr'):
            key = (' '.join(td.text.strip() for td in row.findAll('td')))
            print(key, file=f)

seven_day = soup.find(id='seven-day-forecast')
forecast_items = seven_day.findAll(class_='tombstone-container')
current = forecast_items[0]

period = current.find(class_='period-name').get_text()
short_desc = current.find(class_='short-desc').get_text()
temp = current.find(class_='temp temp-high').get_text()

with open('C:\\Users\\user\\Documents\\Weather\\current-weather.txt', 'a') as f:
	print(period + '\n', short_desc + '\n', temp, file=f)
Reply
#2
Note that the closing python tag needs a slash {/} at the beginning of it ([/python]).

I think your main inefficiency is that you are opening and closing the same file three times. I would have just one with statement, with all of the code under it. If you don't like that much code under the with statement, you could rearrange the code to collect all of the data, and then do all of the file stuff at the end.

I would also do all of the file writing with f.write() instead of print. You will need to explicitly write the end of line characters ('\n'). And name the file something other than f, like weather_file. All of you other variable names are nice and descriptive.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Collect in list then write once.
Example that remove the first two write same with last,and get_text() has become text findAll() --> find_all()
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.findAll(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.findAll('tr'):
        key = (' '.join(td.text.strip() for td in row.find_all('td')))
        weather.append(key)

with open('current-weather.txt', 'w') as f:
    for info in weather:
        f.write(f'{info}\n')
Reply
#4
Sorry for the late reply.
Thank you both for your suggestions and example. I look forward to learning more each day. This language is actually fun to code. Thanks again and have a great day!

~b4iknew
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getting started, again bluedoor5 19 10,587 Jul-23-2018, 06:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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