web-crawling / main.py
pvanand's picture
Update main.py
9300ae8 verified
raw
history blame
738 Bytes
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import hrequests
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
class URLRequest(BaseModel):
url: str
@app.post("/scrape")
async def scrape(url_request: URLRequest):
try:
response = hrequests.get(url_request.url, browser='chrome')
return {"content": response.text}
except Exception as e:
raise e
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/", tags=["Home"])
def api_home():
return {'detail': 'Welcome to Web-Scraping API! Visit https://pvanand-web-scraping.hf.space/docs to test'}