Advertisement
dereksir

Untitled

Aug 25th, 2023 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. base_url = 'https://scrapingclub.com/exercise/list_infinite_scroll/'
  5. page_number = 1  # Start with the base URL
  6. total_pages = 6
  7.  
  8. while True:
  9.     # Construct ajax request URL
  10.     url = f'{base_url}?page={page_number}'
  11.    
  12.     # Make GET request
  13.     response = requests.get(url)
  14.    
  15.     # Retrieve the response content
  16.     html_content = response.text
  17.  
  18.     # Move to the next page
  19.     page_number += 1
  20.  
  21.     if page_number > total_pages:
  22.         break  # Stop the loop
  23.  
  24.     # Parse the HTML content using Beautiful Soup
  25.     soup = BeautifulSoup(html_content, 'html.parser')
  26.  
  27.     # Extract product names and prices
  28.     products = soup.select('div.p-4 h4 > a')
  29.     prices = soup.select('div.p-4 h5')
  30.  
  31.     for product, price in zip(products, prices):
  32.         product_name = product.get_text(strip=True)
  33.         product_price = price.get_text(strip=True)
  34.         print(f'Product: {product_name} | Price: {product_price}')
  35.  
  36.     print('-' * 50)
  37.  
  38.     # Move to the next page
  39.     page_number += 1
  40.  
  41.     if page_number > total_pages:
  42.         break  # Stop the loop
  43.    
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement