In this program, we first import the requests
and BeautifulSoup
packages. We define the URL to scrape as url
, which is the weather forecast page for Mumbai on AccuWeather.
We then send a GET request to the URL using requests.get
and store the response in response
. We parse the HTML content of the response using BeautifulSoup
and store it in soup
.
We then use soup.find
and soup.find_all
to find the current temperature and condition, as well as the daily forecast. We loop over the daily forecast using a for
loop, and use find
and get_text
to extract the day of the week, condition, low temperature, and high temperature for each day.
Finally, we use print
statements to print out the current temperature and condition, as well as the daily forecast.
import requests
from bs4 import BeautifulSoup
Define the URL to scrape
url = “https://www.accuweather.com/en/in/mumbai/204842/weather-forecast/204842”
Send a GET request to the URL and store the response
response = requests.get(url)
Parse the HTML content of the response using BeautifulSoup
soup = BeautifulSoup(response.content, “html.parser”)
Find the current temperature and condition
current_temp = soup.find(“span”, class_=”display-temp”).get_text()
current_cond = soup.find(“span”, class_=”cond”).get_text()
Find the daily forecast
daily_forecast = soup.find(“ul”, class_=”daily”).find_all(“li”)
Print the results
print(“Current temperature in Mumbai is:”, current_temp)
print(“Current condition in Mumbai is:”, current_cond)
print(“Daily forecast for Mumbai:”)
for forecast in daily_forecast:
day = forecast.find(“h3”).get_text()
cond = forecast.find(“span”, class_=”cond”).get_text()
temp_low = forecast.find(“span”, class_=”temp”).find_all(“span”)[0].get_text()
temp_high = forecast.find(“span”, class_=”temp”).find_all(“span”)[2].get_text()
print(day + “:”, cond + “,”, “Low: ” + temp_low + “,”, “High: ” + temp_high)