Collect in list then write once.
Example that remove the first two write same with last,and
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')