Python Web Scraping and create FastAPI

Python Web Scraping and create FastAPI

python requests-html ile basit bir veri çekme operasyonu hazırladım. scraper.py dosyam bu şekilde veri çektiğim site : quotes.toscrape.com/tag/inspirational

from requests_html import HTMLSession

class Scraper():
    def scrapedata(self, tag):
        url = f'https://quotes.toscrape.com/tag/{tag}'
        s = HTMLSession()
        r = s.get(url)
        print(r.status_code)

        qlist = []
        quotes = r.html.find('div.quote')

        for q in quotes:
            item = {
                'text' : q.find('span.text', first=True).text.strip(),
                'author' : q.find('small.author', first=True).text.strip()

            }
            print(item)
            qlist.append(item)
        return qlist

quotes = Scraper()
quotes.scrapedata('life')

FastAPI Daha önce NodeJs kullanarak oluşturduğum api server şimdi python altyapısı ile karşımda. Başlangıç içeriklerini izleyerek 127.0.0.1:8000/books url sine gelen books hakkındaki özlü sözler...

from fastapi import FastAPI
from scraper import Scraper

app = FastAPI()
quotes = Scraper()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/{cat}")
async def read_item(cat):
    return quotes.scrapedata(cat)