Pull to refresh

Что нам стоит сайт распарсить. Основы webdriver API

Reading time 16 min
Views 65K
Поиск жилья, информации о товарах, вакансий, знакомств, сравнение товаров фирмы с конкурентами, исследование отзывов в сети.



В интернет опубликовано много полезной информации и умение извлекать данные поможет в жизни и работе. Научимся получать информацию с помощью webdriver API. В публикации приведу два примера, код которых доступен на github. В конце статьи скринкаст про то, как программа управляет браузером.

Программа или скрипт с помощью вебдрайвера управляет вашим браузером — выполняет ввод текста, переход по ссылкам, щелчок по элементу, извлекает данные из страницы и её элементов, делает скриншоты сайта и т.п.

Для работы webdriver нужно два компонента: браузер/сервер протокола и клиентская часть в виде библиотеки для вашего языка программирования.

Вы можете использовать webdriver API из разных языков программирования и виртуальных машин: официальные клиенты webdriver есть для C#, Ruby, Python, Javascript(Node), а так же клиенты от сообщества для Perl, Perl 6, PHP, Haskell, Objective-C, Javascript, R, Dart, Tcl.

На данный момент Webdriver является стандартом W3C, над которым все еще идет работа. Изначально Webdriver API появился в проекте selenium для целей тестирования, в результате эволюции Selenium-RC API.

В качестве сервера используется отдельный процесс «понимающий» язык протокола. Этот процесс управляет вашим браузером. Есть следующие драйвера:

  • AndroidDriver
  • ChromeDriver
  • FirefoxDriver
  • InternetExplorerDriver
  • SafariDriver

Из этого списка выделяются два драйвера:

  • HtmlUnitDriver — является оберткой для библиотеки HtmlUnit-эмулятора браузера, которая работает в том же java процессе, что и клиентская часть
  • PhantomJSDriver — это браузер на основе webkit без графической части (headless) и драйвер в одном процессе с сервером.

Суть технологии...


Минимум теории для дальнейшей работы. Последовательность действий в клиенте


Итак, наш выбор phantomjs. Это полноценный браузер, который управляется по протоколу webdriver. Можно запускать множество его процессов одновременно, не требуется графическая подсистема, внутри полноценно исполняется javascript (в контраст с ограничениями htmlunit). Если писать сценарии на javascript и передавать его как параметр при старте, то phantomJS может их выполнять и без вебдрайвер протокола и даже доступна отладка с помощью другого браузера.



Описанное ниже относится по большей части к API для java/groovy. В клиентах других языков список функций и параметров должен быть похож.

Получаем сервер с вебдрайвером.


String phantomJsPath = PhantomJsDowloader.getPhantomJsPath()

Загружает phantomjs из репозитария maven, распаковывает и возвращает путь к этому браузеру. Для использования нужно подключить к проекту библиотеку из maven: com.github.igor-suhorukov:phantomjs-runner:1.1.

Этот шаг можно пропустить, если вы предварительно установили в локальную файловую систему вебдрайвер для вашего браузера.

Создаем клиент, соединяемся с сервером


WebDriver driver = new PhantomJSDriver(settings)

Конфигурирует порт для взаимодействию по webdriver протоколу, запускает процесс phantomjs и подключается к нему.

Открываем в браузере нужную страницу


driver.get(url)

Открывает в браузере страницу для заданного адреса.

Получаем интересующу нас информацию


WebElement leftmenu = driver.findElement(By.id("leftmenu"))
List<WebElement> linkList = leftmenu.findElements(By.tagName("a"))

У экземпляра драйвера driver и у элемента, полученного из него, есть два полезных метода: findElement, findElements. Первый возвращает элемент или бросает исключение NoSuchElementException, если элемент не найден. Второй — возвращает коллекцию элементов.

Элементы можно выбирать по следующим запросам org.openqa.selenium.By:

  • id
  • name
  • tagName
  • xpath
  • className
  • cssSelector
  • linkText
  • partialLinkText

Я буду активно использовать id, tagName и xpath. Для не знакомых с xpath — рекомендую разобраться на примерах или по статьям, а только потом перейти к чтению спецификации.

Выполняем действия с элементами на странице и со страницей


С элементом можно вытворять следующее:

  • menuItem.click() — посылает элементу событие клик
  • inputField.sendKeys(«blah-blah») — посылает элементу события нажатия клавиш
  • formButton.submit() — отправляет данные формы, вызывая событие submit

driver.getScreenshotAs(type)

Делает снимок окна браузера. Полезным дополнением к стандартной функции снимка рекомендую библиотеку aShot — она позволяет делать снимок только определенного элемента в окне и позволяет сравнивать элементы как изображения.

Скриншоты можно получить как:

  • OutputType.BASE64 — строку в этом формате, можно например встроить в HTML embedded картинку
  • OutputType.BYTES — массив байт и вертись с ним как умеешь
  • OutputType.FILE — временный файл, для многих инструментов самый удобный способ

Закрываем соединение с браузером


driver.quit()

Закрывает соединение по протоколу и в нашем случае останавливает процесс phantomjs.

Пример 1: Гуляем groovy скриптом по профилям социальной сети


Запустим команду:

java -jar groovy-grape-aether-2.4.5.1.jar crawler.groovy http://??.com/catalog.php

Ссылки на необходимые для запуска файлы:

Скрипт в консоле напечатает путь к html файлу, что создал на основе информации из социальной сети. В странице вы увидите имя пользователя, момент последнего посещения соцсети и скриншот всей страницы пользователя.

Зачем запускать груви скрипт с помощью groovy-grape-aether-2.4.5.1
Про сборку груви groovy-grape-aether-2.4.5.1.jar недавно рассказывал в статье «Уличная магия в скриптах или что связывает Groovy, Ivy и Maven?». Главное отличие от groovy-all-2.4.5.jar — возможность работы механизма Grape с репозитариями более корректным по сравнению с ivy способом, при помощи библиотеки aether, а так же наличие классов доступа к репозитариям в сборке.

crawler.groovy
package com.github.igorsuhorukov.phantomjs

@Grab(group='commons-io', module='commons-io', version='2.2')
import org.apache.commons.io.IOUtils
@Grab(group='com.github.detro', module='phantomjsdriver', version='1.2.0')
import org.openqa.selenium.*
import org.openqa.selenium.phantomjs.PhantomJSDriver
import org.openqa.selenium.phantomjs.PhantomJSDriverService
import org.openqa.selenium.remote.DesiredCapabilities
@Grab(group='com.github.igor-suhorukov', module='phantomjs-runner', version='1.1')
import com.github.igorsuhorukov.phantomjs.PhantomJsDowloader

public class Crawler {
    public static final java.lang.String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

    public static void run(String baseUrl) {

        def phantomJsPath = PhantomJsDowloader.getPhantomJsPath()

        def DesiredCapabilities settings = new DesiredCapabilities()
        settings.setJavascriptEnabled(true)
        settings.setCapability("takesScreenshot", true)
        settings.setCapability("userAgent", com.github.igorsuhorukov.phantomjs.Crawler.USER_AGENT)
        settings.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomJsPath)

        def WebDriver driver = new PhantomJSDriver(settings)

        def randomUrl = null
        def lastVisited=null
        def name=null

        boolean pass=true
        while (pass){
            try {
                randomUrl = getUrl(driver, baseUrl)
                driver.get(randomUrl)
                def titleElement = driver.findElement(By.id("title"))
                lastVisited = titleElement.findElement(By.id("profile_time_lv")).getText()
                name = titleElement.findElement(By.tagName("a")).getText()
                pass=false
            } catch (NoSuchElementException e) {
                System.out.println(e.getMessage()+". Try again.")
            }
        }
        String screenshotAs = driver.getScreenshotAs(OutputType.BASE64)
        File resultFile = File.createTempFile("phantomjs", ".html")
        OutputStreamWriter streamWriter = new OutputStreamWriter(new FileOutputStream(resultFile), "UTF-8")
        IOUtils.write("""<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body>
                                <p>${name}</p><p>${lastVisited}</p>
                                <img alt="Embedded Image" src="data:image/png;base64,${screenshotAs}"></body>
                        </html>""", streamWriter)
        IOUtils.closeQuietly(streamWriter)

        println "html ${resultFile} created"

        driver.quit();
    }

    static String getUrl(WebDriver driver, String baseUrl) {

        driver.get(baseUrl)

        def elements =  driver.findElements(By.xpath("//div[@id='content']//a"))
        def element = elements.get((int) Math.ceil(Math.random() * elements.size()))
        String randomUrl = element.getAttribute("href")
        randomUrl.contains("catalog") ? getUrl(driver, randomUrl) : randomUrl
    }
}

Crawler.run(this.args.getAt(0))

Хорошо знающие груви, заметят что Geb более подходящее решение. Но поскольку оно прячет за своим DSL всю работу с webdriver, то в наших учебных целях Geb не подходит. Из эстетических же соображений я с вами соглашусь!

Пример 2: Извлекаем данные о проектах из java-source программой на java


Пример доступен тут. Для запуска нужна java8, так как используются стримы и try-with-resources.

git clone https://github.com/igor-suhorukov/java-webdriver-example.git
mvn clean package -Dexec.args="http://java-source.net"

В этом примере использую xpath и axis для извлечения информации из страницы. Как пример, фрагмент класса Project.

WebElement main = driver.findElement(By.id("main"));
name = main.findElement(By.tagName("h3")).getText();
description = main.findElement(By.xpath("//h3/following-sibling::table/tbody/tr/td[1]")).getText();
link = main.findElement(By.xpath("//td[text()='HomePage']/following-sibling::*")).getText();
license = main.findElement(By.xpath("//td[text()='License']/following-sibling::*")).getText();

Часть извлеченных из сайта данных. Файла projects.xml - результат работы программы
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<projects source="http://java-source.net">
    <category>
        <category>Open Source Ajax Frameworks</category>
        <project>
            <name>DWR</name>
            <description>DWR is a Java open source library which allows you to write Ajax web sites. It allows code in a browser to use Java functions running on a web server just as if it was in the browser. DWR works by dynamically generating Javascript based on Java classes. The code does some Ajax magic to make it feel like the execution is happening on the browser, but in reality the server is executing the code and DWR is marshalling the data back and forwards.</description>
            <license>Apache Software License</license>
            <link>http://getahead.org/dwr</link>
        </project>
        <project>
            <name>Google Web Toolkit</name>
            <description>Google Web Toolkit (GWT) is an open source Java software development framework that makes writing AJAX applications like Google Maps and Gmail easy for developers who don't speak browser quirks as a second language. Writing dynamic web applications today is a tedious and error-prone process; you spend 90% of your time working around subtle incompatibilities between web browsers and platforms, and JavaScript's lack of modularity makes sharing, testing, and reusing AJAX components difficult and fragile. GWT lets you avoid many of these headaches while offering your users the same dynamic, standards-compliant experience. You write your front end in the Java programming language, and the GWT compiler converts your Java classes to browser-compliant JavaScript and HTML.</description>
            <license>Apache Software License</license>
            <link>http://code.google.com/webtoolkit/</link>
        </project>
        <project>
            <name>Echo2</name>
            <description>Echo2 is the next-generation of the Echo Web Framework, a platform for developing web-based applications that approach the capabilities of rich clients. The 2.0 version holds true to the core concepts of Echo while providing dramatic performance, capability, and user-experience enhancements made possible by its new Ajax-based rendering engine.</description>
            <license>Mozilla Public License</license>
            <link>http://www.nextapp.com/platform/echo2/echo/</link>
        </project>
        <project>
            <name>ICEfaces</name>
            <description>ICEfaces is an integrated Ajax application framework that enables Java EE application developers to easily create and deploy thin-client rich Internet applications (RIA) in pure Java. ICEfaces leverages the entire standards-based Java EE ecosystem of tools and execution environments. Rich enterprise application features are developed in pure Java, and in a pure thin-client model. There are no Applets or proprietary browser plug-ins required. ICEfaces applications are JavaServer Faces (JSF) applications, so Java EE application development skills apply directly and Java developers are isolated from doing any JavaScript related development.</description>
            <license>Mozilla Public License</license>
            <link>http://www.icefaces.org</link>
        </project>
        <project>
            <name>SweetDEV RIA</name>
            <description>SweetDEV RIA is a complete set of world-class Ajax tags in Java/J2EE. It helps you to design Rich GUI in a thin client. SweetDEV RIA provides you Out-Of-The-Box Ajax tags. Continue to develop your application with frameworks like Struts or JSF. SweetDEV RIA tags can be plugged into your JSP pages.</description>
            <license>Apache Software License</license>
            <link>http://sweetdev-ria.ideotechnologies.com</link>
        </project>
        <project>
            <name>ItsNat, Natural AJAX</name>
            <description>ItsNat is an open source (dual licensed GNU Affero General Public License v3/commercial license for closed source projects) Java AJAX Component based Web Framework. It offers a natural approach to the modern Web 2.0 development. ItsNat simulates a Universal W3C Java Browser in the server. The server mimics the behavior of a web browser, containing a W3C DOM Level 2 node tree and receiving W3C DOM Events using AJAX. Every DOM server change is automatically sent to the client and updated the client DOM accordingly. Consequences: pure (X)HTML templates and pure Java W3C DOM for the view logic. No JSP, no custom tags, no XML meta-programming, no expression languages, no black boxed components where the developer has absolute control of the view. ItsNat provides an, optional, event based (AJAX) Component System, inspired in Swing and reusing Swing as far as possible such as data and selection models, where every DOM element or element group can be easily a component.</description>
            <license>GNU General Public License (GPL)</license>
            <link>http://www.itsnat.org</link>
        </project>
        <project>
            <name>ThinWire</name>
            <description>ThinWire is an development framework that allows you to easily build applications for the web that have responsive, expressive and interactive user interfaces without the complexity of the alternatives. While virtually any web application can be built with ThinWire, when it comes to enterprise applications, the framework excels with its highly interactive and rich user interface components.</description>
            <license>GNU Library or Lesser General Public License (LGPL)</license>
            <link>http://www.thinwire.com/</link>
        </project>
    </category>
    <category>
        <category>Open Source Aspect-Oriented Frameworks</category>
        <project>
            <name>AspectJ</name>
            <description>AspectJ is a seamless aspect-oriented extension to the Java programming language, Java platform compatible and easy to learn and use. AspectJ enables the clean modularization of crosscutting concerns such as: error checking and handling, synchronization, context-sensitive behavior, performance optimizations, monitoring and logging, debugging support, multi-object protocols.</description>
            <license>Mozilla Public License</license>
            <link>http://eclipse.org/aspectj/</link>
        </project>
        <project>
            <name>AspectWerkz</name>
            <description>AspectWerkz is a dynamic, lightweight and high-performant AOP framework for Java. AspectWerkz offers both power and simplicity and will help you to easily integrate AOP in both new and existing projects. AspectWerkz utilizes runtime bytecode modification to weave your classes at runtime. It hooks in and weaves classes loaded by any class loader except the bootstrap class loader. It has a rich and highly orthogonal join point model. Aspects, advices and introductions are written in plain Java and your target classes can be regular POJOs. You have the possibility to add, remove and re-structure advice as well as swapping the implementation of your introductions at runtime. Your aspects can be defined using either an XML definition file or using runtime attributes.</description>
            <license>GNU Library or Lesser General Public License (LGPL)</license>
            <link>http://aspectwerkz.codehaus.org/</link>
        </project>
        <project>
            <name>Nanning</name>
            <description>Nanning Aspects is a simple yet scaleable aspect-oriented framework for Java.</description>
            <license>BSD License</license>
            <link>http://nanning.codehaus.org/</link>
        </project>
        <project>
            <name>JBossAOP</name>
            <description>JBoss-AOP allows you to apply interceptor technology and patterns to plain Java classes and Dynamic Proxies. It includes: * Java Class Interception. Field, constructor, and method interception, public, private, protected, and package protected, static and class members. * Fully compositional pointcuts caller side for methods and constructors, control flow, annotations. * Aspect classes Advices can be incapsulated in scoped Java classes * Hot-Deploy. Interceptors can be deployed, undeployed, and redeployed at runtime for both dynamic proxies and classes.(working) * Introductions. The ability to add any arbitrary interface to a Java class. Either an interceptor or a 'mixin' class can service method calls for the attached interfaces. * Dynamic Proxies. The ability to define a dynamic proxy and an interceptor chain for it. Proxies can either be created from an existing class, or from a set of interfaces ala java.lang.reflect.Proxy. * Metadata and Attribute Programming. The ability to define and attach metadata configuration to your classes or dynamic proxies. Interceptors can be triggered when metadata is added to a class. We also have Metadata Chains, the ability to define defaults at the cluster and application level, as well as the ability to override configuration at runtime for a specific method call. * Dynamic AOP. All aspected objects can be typecasted to an AOP api. You can do things like add/remove new interceptors to a specific instance or add/remove instance configuration/metadata at runtime.</description>
            <license>GNU Library or Lesser General Public License (LGPL)</license>
            <link>http://www.jboss.org/products/aop</link>
        </project>
        <project>
            <name>dynaop</name>
            <description>dynaop, a proxy-based Aspect-Oriented Programming (AOP) framework, enhances Object-Oriented (OO) design in the following areas: code reuse decomposition dependency reduction</description>
            <license>Apache Software License</license>
            <link>https://dynaop.dev.java.net/</link>
        </project>
        <project>
            <name>CAESAR</name>
            <description>CAESAR is a new aspect-oriented programming language compatible to Java, that is, all Java programs will run with CAESAR.</description>
            <license>GNU General Public License (GPL)</license>
            <link>http://caesarj.org/</link>
        </project>
        <project>
            <name>EAOP</name>
            <description>Event-based Aspect-Oriented Programming (EAOP) for Java. EAOP 1.0 realizes the EAOP model through the following characteristics: * Expressive crosscuts: execution points can be represented by events and crosscuts can be expressed which denote arbitrary relations between events. * Explicit aspect composition: Aspects may be combined using a (extensible) set of aspect composition operators. * Aspects of aspects: Aspects may be applied to other aspects. * Dynamic aspect management: Aspects may be instantiated, composed and destroyed at runtime.</description>
            <license>GNU General Public License (GPL)</license>
            <link>http://www.emn.fr/x-info/eaop/tool.html</link>
        </project>
        <project>
            <name>JAC</name>
            <description>JAC (Java Aspect Components) is a project consisting in developing an aspect-oriented middleware layer.</description>
            <license>GNU Library or Lesser General Public License (LGPL)</license>
            <link>http://jac.objectweb.org/</link>
        </project>
        <project>
            <name>Colt</name>
            <description>Open Source Libraries for High Performance Scientific and Technical Computing in Java</description>
            <license>The Artistic License</license>
            <link>http://hoschek.home.cern.ch/hoschek/colt/</link>
        </project>
        <project>
            <name>DynamicAspects</name>
            <description>DynamicAspects enables you to do aspect-oriented programming in pure Java. Using the \"instrumentation\" and \"agent\" features introduced with Sun JDK 1.5, aspects can be installed and deinstalled during runtime!</description>
            <license>BSD License</license>
            <link>http://dynamicaspects.sourceforge.net/</link>
        </project>
        <project>
            <name>PROSE</name>
            <description>The PROSE system (PROSE stands for PROgrammable extenSions of sErvices) is a dynamic weaving tool (allows inserting and withdrawing aspects to and from running applications) PROSE aspects are regular JAVA objects that can be sent to and be received from computers on the network. Signatures can be used to guarantee their integrity. Once an aspect has been inserted in a JVM, any occurrence of the events of interest results in the execution of the corresponding aspect advice. If an aspect is withdrawn from the JVM, the aspect code is discarded and the corresponding interception(s) will no longer take place.</description>
            <license>Mozilla Public License</license>
            <link>http://prose.ethz.ch/Wiki.jsp?page=AboutProse</link>
        </project>
        <project>
            <name>Azuki Framework</name>
            <description>The Azuki Framework is a java application framework, designed to reduce the development, deployment and maintenance costs of software systems. The Azuki Framework provides also an unique combination of powerful design patterns (decorator, injection, intercepter, command, proxy...). It provides a rapid application assembly from known components in order to build large systems. The software conception is split into two main stages : * Creation of independent components (technical & business service). * Definition of component dependencies (weaving)</description>
            <license>GNU Library or Lesser General Public License (LGPL)</license>
            <link>http://www.azuki-framework.org/</link>
        </project>
        <project>
            <name>CALI</name>
            <description>CALI is a framework to prototype and compose Aspect-Oriented Programming Languages on top of Java. It is based on an abstract aspect language that its extensible to implement new AOPL. As proof of approach and methodology, the following language have been implemented: -AspectJ (Dynamic part of AspectJ, where intertype declartion can be implemented using regular AspectJ); -EAOPJ : An implementation of Event-Based AOP for Java; -COOL: A DSAL for coordination; -Decorator DSAL. You can use CALI to implement your new AOPL and compose it with existing implementation or using existing implementation to write your applications with aspects form different AOPL.</description>
            <license>Other</license>
            <link>http://www.emn.fr/x-info/cali/</link>
        </project>
    </category>
</projects>


Вот так этот же пример работает с драйвером ChromeDriver (org.seleniumhq.selenium:selenium-chrome-driver:2.48.2). В отличии от PhantomJS в этом случае видно что происходит во время запуска программы: переход по ссылкам, отрисовка страницы.



Вывод


Webdriver API можно использовать из разных языков программирования. Написать скрипт или программу для управления браузером и извлечения информации из страниц достаточно просто: данные из страницы удобно получать по Id тега, CSS селектору или XPath выражению. Возможно делать снимки страницы и отдельных элементов на ней. На основе примеров и документации можно разработать сценарии почти любой сложности для работы с сайтом. Для разработки и отладки лучше использовать обычный браузер и вебдрайвер для него. Для полностью автоматической работы лучше подходит PhantomJS.

Удачи в извлечении открытой и полезной информации из веб!
Tags:
Hubs:
+18
Comments 42
Comments Comments 42

Articles