-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
30 lines (24 loc) · 884 Bytes
/
main.py
File metadata and controls
30 lines (24 loc) · 884 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
29
30
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, HttpUrl
from scraper import fetch_data, extract_data
app = FastAPI()
# A simple homepage endpoint
@app.get('/')
def home():
return {'data': 'Welcome to the Web Scraper API!'}
# Request schema for the scraper
class ScraperRequest(BaseModel):
url: HttpUrl
# The endpoint to accept URLs and return scraped data
@app.post('/scrape')
def scrape_data(request: ScraperRequest):
try:
# Use Selenium to fetch the page content
soup = fetch_data(str(request.url))
# Extract paragraphs and titles
data = extract_data(soup)
# Return the extracted data as JSON
return {"data": data}
except Exception as e:
# Return a 500 error if scraping fails
raise HTTPException(status_code=500, detail=f'An error occurred: {str(e)}')