You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c77c3ba
Browse filesBrowse the repository at this point in the historyBrowse files
Web scraping is a fundamental skill that is extremely useful for data collection and automating tasks. The following examples will show how we scrape sites such as [wrapbootstrap](https://wrapbootstrap.com/?ref=stubbornjava) and [themeforest](https://themeforest.net/?ref=StubbornJava) to populate the [HTML/CSS Theme Templates](/best-selling-html-css-themes-and-website-templates) page. We will be using [jsoup](https://jsoup.org/) for DOM parsing and [OkHttp](http://square.github.io/okhttp/) for HTTP. Although jsoup is capable of handling HTTP for us we prefer to stick with OkHttp incase we need anything more complex than a simple GET request such as special headers and cookies. Why learn two libraries when one will do?
4
+
{{/assign}}
5
+
{{mdmarkdown}}
6
+
</div>
7
+
8
+
<h2class="anchored">Model / POJO</h2>
9
+
</p>We like to start simple so we are only gathering four fields title, url, image url, and number of downloads if available.</p>
<p>Our scraper is fairly simple. All it needs to do is a single GET request and extract the data we are interested in. We are using <ahref="https://github.com/jhalterman/failsafe#retries">failsafe</a> for retry logic and <ahref="https://github.com/jOOQ/jOOL">jOOλ</a> for a simplified streaming api. Setting up <ahref="/posts/okhttpclient-logging-configuration-with-interceptors">OkHttpClient Logging Interceptors</a> is very useful for tracking down bugs. We are only showing the <ahref="https://wrapbootstrap.com/?ref=stubbornjava">wrapbootstrap</a> scraper but the rest can be found <ahref="https://github.com/StubbornJava/StubbornJava/tree/master/stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/themes">here</a>.</p>
<p>Our naming convention for the service layer is generally jut pluralizing the model. We don't care how it's getting the data as long as it gets it. We are caching the results of each scraper so we don't upset the websites maintainers. In a more ideal world we might periodically scrape and store the data in our own database.</p>
<p>Now we simply create a custom <ahref="/posts/undertow-writing-custom-httphandlers">HttpHandler</a> and pass the themes along to the HTML template.</p>
<p>Finally its hooked into our <ahref="https://github.com/StubbornJava/StubbornJava/blob/master/stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/StubbornJavaWebApp.java#L58">router</a> and now we have a functioning <ahref="/best-selling-html-css-themes-and-website-templates">HTML / CSS Theme Template</a> page.
0 commit comments