Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting started
#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


Messages In This Thread
getting started - by b4iknew - Jan-20-2019, 02:27 PM
RE: getting started - by ichabod801 - Jan-20-2019, 03:51 PM
RE: getting started - by snippsat - Jan-21-2019, 04:20 AM
RE: getting started - by b4iknew - Jan-22-2019, 09:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  getting started, again bluedoor5 19 10,670 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