-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-app.py
37 lines (29 loc) · 1.14 KB
/
main-app.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
import requests
from bs4 import BeautifulSoup
import json
from fastapi import FastAPI
app = FastAPI()
@app.get("/weather/{id}")
def get_weather(id: int):
# Send a GET request to the URL with the provided ID
url = f"https://city.imd.gov.in/citywx/city_weather_test.php?id={id}"
response = requests.get(url)
# Parse the HTML content
soup = BeautifulSoup(response.content, 'html.parser')
# Find the table containing the past 24 hours weather data
table = soup.find('table', attrs={'cellpadding': '0', 'cellspacing': '0', 'width': '100%'})
# Find all the rows within the table
rows = table.find_all('tr')
details = {}
# Iterate over the rows and extract the data
for row in rows:
data = row.find_all('td')
if len(data) == 2: # Check if the row contains two columns of data
label = data[0].text.strip()
value = data[1].text.strip()
keyword = label.split('(')[0].strip()
details[keyword] = value
return details # Return the details dictionary as JSON response
@app.get("/{path}")
def handle_404(path: str):
return {"message": "Not found"}