-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
57 lines (40 loc) · 1.15 KB
/
app.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import requests
from playwright.sync_api import sync_playwright
# TODO: make it async
def test_urls(urls):
for url in urls:
try:
resp = requests.get(url)
resp.raise_for_status()
print(f"{url} IS OK")
except Exception as e:
print(e)
print(f"{url} FAILED!")
def run(playwright):
firefox_path = os.environ.get("FIREFOX_PATH")
browser = None
try:
browser = playwright.firefox.launch(
executable_path=firefox_path, headless=False
)
page = browser.new_page()
page.goto("https://boto.io")
links = page.query_selector_all('a:visible')
hrefs = [
l.get_attribute("href") for l in links
]
# close after all operations on the page.
browser.close()
external_hrefs = [
h for h in hrefs if h[:4] == "http"
]
return external_hrefs
except Exception:
if browser:
browser.close()
raise
def handler(event, context):
with sync_playwright() as playwright:
urls = run(playwright)
test_urls(urls)