绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Java. How to use headless browsers for crawling we
2020-01-10 17:55:23

ttps://www.linkedin.com/pulse/java-how-use-headless-browsers-crawling-web-scraping->Did you ever think to implement software to scrape data from web pages? I guess everyone could think about crawling web.

The simplest way to get data from remote page is run your preferable web browser, load target web page, select needed text, copy and past text into text editor for the following data transformations. Joke :)

To be honest how to automate this routine process?  Let's determine primary tasks need to be solved for implementing our crawler.

  1. Load data from remote host. It is not a secret how to to this...
  2. Parse loaded html and build DOM (Document Object Model).
  3. Get data by traversing DOM or using CSS selectors. 
  4. Save or pass data for other tasks.

Parsing static HTML is quite "easy task". There are Java libraries which do this task very well. I would recommend to take a look at http://jsoup.org It's enough in simple case. 

How to be with hidden HTML which is created by Javascript? We need to use browser or implement browser :) Fortunately we do not have to implement  our own browser if we want just to implement crawler. These browsers are already implemented. Our herous: http://phantomjs.orghttps://slimerjs.org

How to organize communication between Java program and headless browser? On the stage appears "Ghost" driver. The both browsers support this driver out of the box. Ghost driver is "relative" of WebDriverWebDriver is well known among test-engineers - a lot  of code examples and manuals. We are free to use Maven for integration GHost driver into crawler application. 

There are difference between http://phantomjs.orghttps://slimerjs.org. It is well documented on FAQ page of Slimerjs project.

Makes sense to consider Javascript framework casperjs.org - is a navigation scripting & testing utility for PhantomJS and SlimerJS written in Javascript.

What if we do not want to use not PhantomJS nor SlimerJS? There are alternatives:

At this point I propose to make a pause. Now we have enough information to dive into implementing of web crawlers applications.

Analytics starts from data gulps :)

Please like and share if you find my arcticle usefull :-)

架构师日常笔记

分享好友

分享这个小栈给你的朋友们,一起进步吧。

运维部落
创建时间:2019-09-15 22:54:27
关于运维,你想知道的,这里都有
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • stanleylst
    栈主

小栈成员

查看更多
  • 小尾巴鱼
  • Cyj_me
  • lpysky
  • 栈栈
戳我,来吐槽~