Update playwright/README.md
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
```python
|
```python
|
||||||
# main.py
|
# play.py
|
||||||
from playwright.async_api import async_playwright as aP
|
from playwright.async_api import async_playwright as aP
|
||||||
import xvfbwrapper, io, os
|
import xvfbwrapper, io, os
|
||||||
from db import DB
|
from db import DB
|
||||||
@@ -14,15 +14,17 @@ async def Page(browser='chromium', headless=True):
|
|||||||
context = await browser.new_context(accept_downloads=True)
|
context = await browser.new_context(accept_downloads=True)
|
||||||
context.set_default_timeout(0)
|
context.set_default_timeout(0)
|
||||||
|
|
||||||
async def handle_request(route):
|
async def save(response):
|
||||||
url = route.request.url
|
if response.ok and not db.exists(url := response.url):
|
||||||
if body := db[url]: return await route.fulfill(body=body)
|
|
||||||
if response := await route.continue_():
|
|
||||||
if response.ok and not db.exists(url):
|
|
||||||
db[url] = await response.body()
|
db[url] = await response.body()
|
||||||
await route.fulfill(response=response)
|
|
||||||
|
|
||||||
await context.route('**/*', handle_request)
|
async def load(route):
|
||||||
|
if body := db[route.request.url]:
|
||||||
|
return await route.fulfill(body=body)
|
||||||
|
await route.continue_()
|
||||||
|
|
||||||
|
context.on('response', save)
|
||||||
|
await context.route('**/*', load)
|
||||||
for block in ['**/*.gif', '**/css*.js']:
|
for block in ['**/*.gif', '**/css*.js']:
|
||||||
await context.route(block, lambda route: route.abort())
|
await context.route(block, lambda route: route.abort())
|
||||||
|
|
||||||
@@ -36,10 +38,11 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# db.py
|
# db.py
|
||||||
|
# db.py
|
||||||
import sqlite3, json
|
import sqlite3, json
|
||||||
|
|
||||||
class DB(sqlite3.Connection):
|
class DB(sqlite3.Connection):
|
||||||
def __init__(self, db_name="db.sqlite"):
|
def __init__(self, db_name=".db.sqlite"):
|
||||||
super().__init__(db_name)
|
super().__init__(db_name)
|
||||||
with self:
|
with self:
|
||||||
self.execute('''
|
self.execute('''
|
||||||
@@ -97,4 +100,31 @@ class DB(sqlite3.Connection):
|
|||||||
LIMIT 1
|
LIMIT 1
|
||||||
''', (key,))
|
''', (key,))
|
||||||
return bool(cur.fetchone())
|
return bool(cur.fetchone())
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
db = DB()
|
||||||
|
|
||||||
|
# 문자열 저장 및 조회
|
||||||
|
db['hello'] = 'world'
|
||||||
|
print(db['hello']) # 출력: world (str 타입)
|
||||||
|
|
||||||
|
# 숫자 저장 및 조회
|
||||||
|
db['number'] = 42
|
||||||
|
print(db['number']) # 출력: 42 (int 타입)
|
||||||
|
|
||||||
|
# 바이너리 데이터 저장 및 조회
|
||||||
|
db['binary'] = b'binary data'
|
||||||
|
print(db['binary']) # 출력: b'binary data' (bytes 타입)
|
||||||
|
|
||||||
|
# 복잡한 객체 저장
|
||||||
|
complex_obj = {'name': 'John', 'age': 30, 'city': 'New York'}
|
||||||
|
db['complex'] = complex_obj
|
||||||
|
loaded_obj = db['complex']
|
||||||
|
print(loaded_obj) # 출력: {'name': 'John', 'age': 30, 'city': 'New York'} (dict 타입)
|
||||||
|
|
||||||
|
db['test_key'] = 'test_value'
|
||||||
|
|
||||||
|
print(db.exists('test_key')) # True
|
||||||
|
print(db.exists('non_existent_key')) # False
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user