Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Data Science Cookbook

You're reading from   Java Data Science Cookbook Explore the power of MLlib, DL4j, Weka, and more

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787122536
Length 372 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Rushdi Shams Rushdi Shams
Author Profile Icon Rushdi Shams
Rushdi Shams
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Obtaining and Cleaning Data FREE CHAPTER 2. Indexing and Searching Data 3. Analyzing Data Statistically 4. Learning from Data - Part 1 5. Learning from Data - Part 2 6. Retrieving Information from Text Data 7. Handling Big Data 8. Learn Deeply from Data 9. Visualizing Data

Extracting web data from a website using Selenium Webdriver

Selenium is a Java-based tool to help automating software testing or quality assurance. Interestingly enough, Selenium can be used to automatically retrieve and utilize web data. This recipe shows you how.

Getting ready

In order to perform this recipe, we will require the following:

  1. Download selenium-server-standalone-2.53.1.jar and selenium-java-2.53.1.zip from http://selenium-release.storage.googleapis.com/index.html?path=2.53/. From the latter, extract the selenium-java-2.53.1.jar file. Include these two JAR files in your eclipse project an external Java library.
  2. Download and install Firefox 47.0.1 from https://ftp.mozilla.org/pub/firefox/releases/47.0.1/ by selecting the version appropriate for your operating system.

Tip

Because of the version conflict issues between Selenium and Firefox, once you run code with a particular version, turn off the automatic update download and installation option in Firefox.

How to do it...

  1. Create a method named extractDataWithSelenium(String) that takes a String as a parameter, which eventually is the URL from where we are going to extract data. There can be many different types of data that we can extract from URLs, such as the title, the headers, and the values in a selection drop-down box. This recipe only concentrates on extracting the text part of the webpage:
            public String extractDataWithSelenium(String url){ 
    
  2. Next, create a Firefox web driver using the following code:
            WebDriver driver = new FirefoxDriver(); 
    
  3. Use the get() method of the WebDriver object by passing the URL:
            driver.get("http://cogenglab.csd.uwo.ca/rushdi.htm"); 
    
  4. The text of the webpage can be found using xpath, where the value of id is content:

    How to do it...

    How to do it...

  5. Find this particular element with the findElement() method. This method returns a WebElement object. Create a WebElement object named webElement to hold the returned value:
            WebElement webElement = driver.findElement(By.xpath("//* 
              [@id='content']")); 
    
  6. The WebElement object has a method named getText(). Call this method to retrieve the text of the web page, and put the text into a String variable as follows:
            String text = (webElement.getText()); 
    
  7. Finally, return the String variable and close the method:
            }

    The complete code segment with the driver main() method for the recipe looks like the following:

    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
     
    public class TestSelenium { 
       public String extractDataWithSelenium(String url) { 
          WebDriver driver = new FirefoxDriver(); 
          driver.get("http://cogenglab.csd.uwo.ca/rushdi.htm"); 
          WebElement webElement = driver.findElement(By.xpath("//*
            [@id='content']")); 
          System.out.println(webElement.getText()); 
          return url;    
       } 
     
       public static void main(String[] args){ 
          TestSelenium test = new TestSelenium(); 
          String webData = test.extractDataWithSelenium
            ("http://cogenglab.csd.uwo.ca/rushdi.htm"); 
          //process webData 
       } 
    } 
    

Note

Selenium and Firefox have compatibility issues. Some Selenium versions do not work with some Firefox versions. The recipe provided here works fine with the versions mentioned in the recipe. But it does not have any guarantee that it will work with other Selenium or Firefox versions.

Because of the version conflict issues between Selenium and Firefox, once you run a code with a particular version of the both, turn off the automatic update download and installation option in Firefox.

You have been reading a chapter from
Java Data Science Cookbook
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787122536
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime