GithubHelp home page GithubHelp logo

oxylabs / web-scraping-selenium-python Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 9 KB

Web Scraping with Python Selenium: Tutorial for Beginners

Python 100.00%
selenium-web-scraper web-scraping github-python json-database-python python-ecommerce scraper-python serp-api-python python-web-crawler web-scraping-python

web-scraping-selenium-python's Introduction

Web Scraping with Python Selenium: Tutorial for Beginners

Oxylabs promo code

In this article, we’ll cover an overview of web scraping with Selenium using a real-life example.

For a detailed tutorial on Selenium, see our blog.

Installing Selenium

  1. Create a virtual environment:
python3 -m venv .env
  1. Install Selenium using pip:
pip install selenium
  1. Install Selenium Web Driver. See this page for details.

Testing

With virtual environment activated, enter IDLE by typing in python3. Enter the following command on IDLE:

>>> from selenium.webdriver import Chrome

If there are no errors, move on to the next step. If there is an error, ensure that chromedriver is added to the PATH.

Scraping with Selenium

Import required modules as follows:

from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By

Add the skeleton of the script as follows:

def get_data(url) -> list:
   ...


def main():
    ...

if __name__ == '__main__':
    main()

Create ChromeOptions object and set headless to True. Use this to create an instance of Chrome.

    browser_options = ChromeOptions()
    browser_options.headless = True

    driver = Chrome(options=browser_options)

Call the driver.get method to load a URL. After that, locate the link for the Humor section by link text and click it:

    driver.get(url)

    element = driver.find_element(By.LINK_TEXT, "Humor")
    element.click()

Create a CSS selector to find all books from this page. After that run a loop on the books and find the bookt title, price, stock availability. Use a dictionary to store one book information and add all these dictionaries to a list. See the code below:

 books = driver.find_elements(By.CSS_SELECTOR, ".product_pod")
    data = []
    for book in books:
        title = book.find_element(By.CSS_SELECTOR, "h3 > a")
        price = book.find_element(By.CSS_SELECTOR, ".price_color")
        stock = book.find_element(By.CSS_SELECTOR, ".instock.availability")
        book_item = {
            'title': title.get_attribute("title"),
            'price': price.text,
            'stock': stock. text
        }
        data.append(book_item)

Lastly, return the data dictionary from this function.

For the complete code, see main.py.

For a detailed tutorial on Selenium, see our blog.

web-scraping-selenium-python's People

Contributors

augustoxy avatar oxyjohan avatar oxylabsorg avatar

Stargazers

 avatar  avatar

Watchers

 avatar

web-scraping-selenium-python's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.