Web scraping script for a custom website

In this post, I will show you how to write a script using selenium that can perform desired actions on any webpage.

We will need the URL of the target page and a stable internet connection. 

Procedure

Install and import the required dependencies:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

Define the URL of the target website

 

url = "https://remarketing.jyskefinans.dk/cars/?maerke=Citro%C3%ABn&model=C3"

Initialize the URL, load the URL and set a timer of a few seconds to wait for the site to load completely. 

driver = webdriver.Chrome()
driver.get(url)
time.sleep(2)

Define a try/catch block to accept the cookies of the webpage before proceeding to ensure a smooth operation. 

try:

    # Find the accept all button and click it

   

    accept_button = driver.find_element(By.CLASS_NAME, "coi-banner__accept")

    accept_button.click()




except:

    pass

Inspect the webpage while coding and search the container/div/button that is to be targeted. 

try:

    while True:

        # Find the div element by its class name

        div_element = driver.find_element(By.CLASS_NAME, "vehicles-export-container")

        # Click the div element

        div_element.click()

        # Wait for 5 seconds

        time.sleep(5)

The above section is the crux of the whole script. Basic actions like click, drag etc can be achieved by making a few modifications in this block of code. 

Refer to this documentation of selenium to find more attributes of the driver() method and how it can interact with webpages:

https://www.selenium.dev/documentation/webdriver/elements/finders/

 

The final script file would look something like this:

from selenium import webdriver

from selenium.webdriver.common.by import By

import time

url = "https://remarketing.jyskefinans.dk/cars/?maerke=Citro%C3%ABn&model=C3"

# Initialize WebDriver

driver = webdriver.Chrome()

# Open the URL

driver.get(url)

# Wait for the cookie popup to appear (assuming it's an overlay)

time.sleep(2)  # Adjust this delay as needed to ensure the popup is fully loaded

try:

    # Find the accept all button and click it

   

    accept_button = driver.find_element(By.CLASS_NAME, "coi-banner__accept")

    accept_button.click()

except:

    # If the accept button is not found or if there's any error, continue without accepting cookies

    pass

try:

    while True:

        # Find the div element by its class name

        div_element = driver.find_element(By.CLASS_NAME, "vehicles-export-container")

        # Click the div element

        div_element.click()

        # Wait for 5 seconds

        time.sleep(5)

except KeyboardInterrupt:

    # Quit the WebDriver if a keyboard interrupt (Ctrl+C) is received

    driver.quit()

Dharmik Valani

Dharmik Valani

Dharmik valani is the full-stack developer with a passion for crafting innovative solutions. With a robust skill set encompassing both front-end and back-end technologies, they bring a wealth of expertise to our platform. Stay tuned for insightful articles, as this author navigates the ever-evolving landscape of web development, sharing valuable insights and best practices.

Dharmik Valani

Dharmik Valani

Dharmik valani is the full-stack developer with a passion for crafting innovative solutions. With a robust skill set encompassing both front-end and back-end technologies, they bring a wealth of expertise to our platform. Stay tuned for insightful articles, as this author navigates the ever-evolving landscape of web development, sharing valuable insights and best practices.

Related Posts

Join the conversation

Connect with us for engaging discussions on captivating content!

Shopping Basket