-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather scraper.py
More file actions
29 lines (20 loc) · 921 Bytes
/
weather scraper.py
File metadata and controls
29 lines (20 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from bs4 import BeautifulSoup as bs
import requests
from time import sleep
# sending request to the website
city = input("Enter the city:\n")
url = f"https://www.google.com/search?q=weather+{city}"
content = requests.get(url).text
# print(url)
soup = bs(content, 'html.parser')
# with open(r"projects\temperture.html",'r',encoding='utf-8') as f:
# content = f.read()
loc = soup.find('span',class_="BNeawe tAd8D AP7Wnd") # the location in better format
print(f"Showing results for : {loc.get_text().strip()} ....")
sleep(2)
tem = soup.find('div', attrs={'class':"BNeawe iBp4i AP7Wnd"}).get_text() # the temperature
print(f"Temperature : {tem.strip()}")
sky = soup.find('div',attrs={'class':"BNeawe tAd8D AP7Wnd"}).get_text().strip() # it will contain the sky's report
sky = sky.split('\n')
# print(sky)
print(f"Date and time : {sky[0]}\nSky : {sky[1]}") # printing the sky and time by indexing into the list