0% found this document useful (0 votes)
12 views1 page

Weather Scraper

Uploaded by

SiddharthBiju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Weather Scraper

Uploaded by

SiddharthBiju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import requests

from bs4 import BeautifulSoup

def get_weather_data(city):
try:
url = f"https://www.weather.com/weather/today/l/{city}"
response = requests.get(url)
response.raise_for_status()

soup = BeautifulSoup(response.text, 'html.parser')

location = soup.find(
'h1', class_='CurrentConditions--location--1Ayv3').text
temperature = soup.find(
'span', class_='CurrentConditions--tempValue--3KcTQ').text
condition = soup.find(
'div', class_='CurrentConditions--phraseValue--2xXSr').text

return f"Location: {location}\nTemperature: {temperature}\nCondition:


{condition}"
except Exception as e:
return f"Error retrieving weather data: {e}"

if __name__ == "__main__":
city_code = "USCA0286:1:US" # Example city code for San Francisco
print(get_weather_data(city_code))

You might also like