-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweathergilgit.py
65 lines (39 loc) · 1.94 KB
/
weathergilgit.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
from bs4 import BeautifulSoup
from datetime import datetime
def scrape_dataGilgit(url):
response = requests.get(url)
htmlContent = response.content
soup = BeautifulSoup(htmlContent, 'html.parser')
gilgit_div = soup.find('div', class_='tab-temp')
gilgitLow = gilgit_div.find('span', class_='tab-temp-low')['data-value']
tr_element = soup.find('tr', class_='step-temp')
gilgitHigh = tr_element.find('td', headers='d0Temp').div.get_text(strip=True) if tr_element and tr_element.find('td', headers='d0Temp') and tr_element.find('td', headers='d0Temp').div else None
gilgitHigh = gilgitHigh.replace('°', '')
gilgitHigh = int(gilgitHigh)
gilgitLow = int(float(gilgitLow))
today_date = datetime.now().date()
span_id = f"tabSummaryText{today_date.strftime('%Y-%m-%d')}"
span_element = soup.find('div', id=span_id)
data_inside_span = span_element.get_text(strip=True) if span_element and span_element.div else None
return {
'gilgit High Temperature': f"{gilgitHigh}°C",
'gilgit Low Temperature': f"{gilgitLow}°C",
'Today\'s Day': today_date.strftime('%d'),
'Data Inside Span': data_inside_span
}
def precipitationGilgit(url):
response = requests.get(url)
htmlContent = response.content
soup = BeautifulSoup(htmlContent, 'html.parser')
tr_element = soup.find('tr', class_='step-pop')
rain = tr_element.find('td', headers='d0PoP').get_text(strip=True) if tr_element and tr_element.find('td', headers='d0PoP') else None
return rain
today_date = datetime.now().date()
day_of_month = today_date.strftime('%d')
url = f"https://www.metoffice.gov.uk/weather/forecast/twku77j3n#?date={today_date.strftime('%Y-%m-%d')}"
scraped_data = scrape_dataGilgit(url)
rain = precipitationGilgit(url)
gilgitHigh = scraped_data['gilgit High Temperature']
gilgitLow = scraped_data['gilgit Low Temperature']
condition = scraped_data['Data Inside Span']