Advertisement
dereksir

Untitled

Nov 23rd, 2023 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. import requests
  2. import time
  3. import aiohttp
  4. import asyncio
  5.  
  6. # Function to retrieve HTML content for synchronous web scraping
  7. async def fetch_page_async(session, url):
  8.     async with session.get(url) as response:
  9.         return await response.text()
  10.  
  11. # Function to retrieve HTML content for asynchronous web scraping
  12. def fetch_page_sync(url):
  13.     response = requests.get(url)
  14.     return response.text
  15.  
  16. # Function to scrape multiple pages synchronously and return time taken
  17. def synchronous_scraper(urls):
  18.     start_time = time.time()
  19.     for url in urls:
  20.         fetch_page_sync(url)
  21.     end_time = time.time()
  22.     return end_time - start_time
  23.  
  24. # Function to scrape multiple pages asynchronously and return time taken
  25. async def asynchronous_scraper(urls):
  26.     start_time = time.time()
  27.     async with aiohttp.ClientSession() as session:
  28.         tasks = [fetch_page_async(session, url) for url in urls]
  29.         await asyncio.gather(*tasks)
  30.     end_time = time.time()
  31.     return end_time - start_time
  32.  
  33. # URL lists
  34. urls = ["https://scrapeme.live/shop/", "https://scrapeme.live/shop/page/2/", "https://scrapeme.live/shop/page/3/"]
  35.  
  36. # Benchmark
  37. synchronous_time = synchronous_scraper(urls)
  38. asynchronous_time = asyncio.run(asynchronous_scraper(urls))
  39.  
  40. print(f"Synchronous Time: {synchronous_time} seconds")
  41. print(f"Asynchronous Time: {asynchronous_time} seconds")
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement