-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsnselectolax.py
More file actions
40 lines (34 loc) · 1.05 KB
/
httpsnselectolax.py
File metadata and controls
40 lines (34 loc) · 1.05 KB
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
import httpx
import asyncio
from selectolax.parser import HTMLParser
import time
import requests
from bs4 import BeautifulSoup
import time
urls = [
# add your URLs here
]
# httpx + selectolax (async)
async def fetch(url, client):
response = await client.get(url)
tree = HTMLParser(response.text)
title_node = tree.css_first("title")
title = title_node.text() if title_node else "No title"
print(f"{url}: {title}")
async def main():
async with httpx.AsyncClient() as client:
tasks = list(map(lambda x: fetch(x, client),urls))
await asyncio.gather(*tasks)
start = time.time()
asyncio.run(main())
end = time.time()
print(f"Total time (httpx + selectolax async): {end - start:.2f} seconds")
# requests + BeautifulSoup
start = time.time()
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
title = soup.title.string if soup.title else "No title"
print(f"{url}: {title}")
end = time.time()
print(f"Total time (requests + BeautifulSoup): {end - start:.2f} seconds")