Question

Hello, I am writing a Python program that displays weather information by importing Beautiful Soup and...

Hello, I am writing a Python program that displays weather information by importing Beautiful Soup and the Requests modules, and using a process called web scraping.

Everything else displays correctly when I call it to the print()function, except for the elevation; when I print it out, I only get "Elev: " - But I only need the number for elevation (in feet), not the Elev: or any other text/or tags before it. e.x.is: instead of the print function displaying "Elev: 2592ft" - I get "Elev: " - when print(weatherStationEle) is called.

Here is what I have written so far:

import bs4, requests

try:
url = 'https://forecast.weather.gov/MapClick.php?'
latAndLon = 'lat=46.8837&lon=-102.7817'
res = requests.get(url + latAndLon)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
wStation = soup.select('#current-conditions > div.panel-heading > div > h2')
cTemp = soup.select('#current_conditions-summary > p.myforecast-current-lrg')
cConditions = soup.select('#current_conditions-summary > p.myforecast-current')
Ele = soup.select('#current-conditions > div.panel-heading > div > span > b:nth-child(3)')
WeatherStationN = wStation[0].text
currentT = cTemp[0].text
cCond = cConditions[0].text
weatherStationEle = Ele[0].text
print(WeatherStationN)
print(currentT)
print(cCond)
print(weatherStationEle)
  
except Exception as ex:
# Print out any error messages
print('An error occurred')

Any help would be greatly appreciated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXPLANATION :

Hi, direct come to the point, in the variable Ele,

Ele = soup.select('#current-conditions > div.panel-heading > div > span > b:nth-child(3)')

you are using E[0].text , so it will give text bounded between the <b> tag so here only, <b> this text will be given </b>, but height 2592ft is present just after closing tag </b>, so it was only fetching <b>Ele :

</b> and not the 2592ft.

Thus what i did is took Ele variable with whole span tag and not going into b,

like Ele = soup.select('#current-conditions > div.panel-heading > div > span')

then it used .contents[] function and iterated to get the desired output.

since it was the last one so i directly used Ele[0].contents[-1] ,

CODE:

import bs4
import requests

try:
    url = 'https://forecast.weather.gov/MapClick.php?'
    latAndLon = 'lat=46.8837&lon=-102.7817'
    res = requests.get(url + latAndLon)
    res.raise_for_status()
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    wStation = soup.select('#current-conditions > div.panel-heading > div > h2')
    cTemp = soup.select('#current_conditions-summary > p.myforecast-current-lrg')
    cConditions = soup.select('#current_conditions-summary > p.myforecast-current')
    Ele = soup.select('#current-conditions > div.panel-heading > div > span ')
    WeatherStationN = wStation[0].text
    currentT = cTemp[0].text
    cCond = cConditions[0].text
    weatherStationEle = Ele[0].contents[-1]     # -1 for the last one i.e., 2592ft
    print(WeatherStationN)
    print(currentT)
    print(cCond)
    print(weatherStationEle)

except Exception as ex:
    print('An error occurred')

OUTPUT :

THANK YOU....!!

Add a comment
Know the answer?
Add Answer to:
Hello, I am writing a Python program that displays weather information by importing Beautiful Soup and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT