Advertisement
dereksir

Untitled

Nov 23rd, 2023 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import aiohttp
  2. import asyncio
  3. import time
  4. from bs4 import BeautifulSoup
  5.  
  6.  
  7. async def fetch_page(session, url):
  8.     # Make GET request using session
  9.     async with session.get(url) as response:
  10.         # Retrieve HTML content
  11.         html_content = await response.text()
  12.         # Parse HTML content using BeautifulSoup
  13.         soup = BeautifulSoup(html_content, 'html.parser')
  14.         # Return parsed HTML
  15.         return soup
  16.  
  17.  
  18. async def main():
  19.     # Initialize a list of URLs
  20.     urls = ["https://scrapeme.live/shop/", "https://scrapeme.live/shop/page/2/", "https://scrapeme.live/shop/page/3/"]
  21.  
  22.     # Time Tracking: Start Time
  23.     start_time = time.time()
  24.  
  25.     # Create an AIOHTTP session
  26.     async with aiohttp.ClientSession() as session:
  27.  
  28.         # Initialize tasks list
  29.         tasks = []
  30.  
  31.         # Loop through URLs and append tasks
  32.         for url in urls:
  33.             tasks.append(fetch_page(session, url))
  34.  
  35.         # Group and Execute tasks concurrently
  36.         htmls = await asyncio.gather(*tasks)
  37.  
  38.     # Time Tracking: End Time
  39.     end_time = time.time()
  40.  
  41.     # Process the extracted information
  42.     for url, soup in zip(urls, htmls):
  43.         # Find products in <ul>
  44.         product_list = soup.find('ul', class_='products')
  45.         # Find all <li>s
  46.         products = product_list.find_all('li')
  47.         # Iterate through products and extract name, price, and image of each.
  48.         for product in products:
  49.             name = product.find('h2').text
  50.             price = product.find('span', class_='amount').text
  51.             image = product.find('img')['src']
  52.  
  53.             print(f"Product from {url}:\nName: {name}\nPrice: {price}\nImage: {image}\n")
  54.  
  55.     # Calculate and print the time taken
  56.     print(f"Time taken: {end_time - start_time} seconds")
  57.  
  58. # Run the main function
  59. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement