Advertisement
dereksir

Untitled

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