-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
33 lines (26 loc) · 1.01 KB
/
api.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
# examples/server_simple.py
import asyncio
import aiohttp
from aiohttp import web
import selectorlib
from selectorlib.formatter import Formatter
class Price(Formatter):
def format(self, text):
price = text.replace('£','').strip()
return float(price)
product_page_extractor = selectorlib.Extractor.from_yaml_file('ProductPage_with_Formatter.yml',formatters = [Price])
async def get_product_page(request):
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
product_url = request.rel_url.query['product_url']
data = {'error':'Please provide a URL'}
if product_url:
html = await fetch(session, product_url)
data = product_page_extractor.extract(html)
return web.json_response(data)
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
app = web.Application()
app.add_routes([web.get('/', get_product_page)])
if __name__ == '__main__':
web.run_app(app)