Mitrahsoft
  • SERVICES
    • OUR SERVICES
      • SOFTWARE DEVELOPMENT
      • MOBILE DEVELOPMENT
      • BLOCKCHAIN DEVELOPMENT
      • SOFTWARE TESTING
      • CLOUD & DEVOPS SERVICES
      • PRODUCT DEVELOPMENT
      • OFFSHORE DEVELOPMENT
      • SOCIAL MEDIA MARKETING
      TECHNOLOGIES
      • COLDFUSION DEVELOPMENT
      • REACTJS DEVELOPMENT
      • VUEJS DEVELOPMENT
      • ANGULAR DEVELOPMENT
      • NODEJS DEVELOPMENT
      • PYTHON DEVELOPMENT
      • GOLANG DEVELOPMENT
      • GOLANG CORPORATE TRAINING
      COLDFUSION EXPERTISE
      • MURACMS EXPERTISE
      • COLDFUSION APP MIGRATION
      • COLDFUSION HOSTING & SUPPORT
      • CF LEGACY APP MAINTENANCE
      • COLDFUSION REST API
      • ECOMMERCE / SHOPPING CART SOLUTIONS
      • PRESIDECMS EXPERTISE
      MOBILE DEVELOPMENT
      • REACT NATIVE DEVELOPMENT
      • FLUTTER DEVELOPMENT
      • IONIC DEVELOPMENT
      • PHONEGAP DEVELOPMENT
  • ABOUT US
    • ABOUT MITRAHSOFT
    • OUR CLIENTS
    • TESTIMONIALS
    • PORTFOLIO
    • CAREERS
  • CONTACT US
  • BLOG
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
Adobe Solution PartnerLucee Solution Partner
  • OUR SERVICESSOFTWARE DEVELOPMENTMOBILE DEVELOPMENTBLOCKCHAIN DEVELOPMENTSOFTWARE TESTINGCLOUD & DEVOPS SERVICESPRODUCT DEVELOPMENTOFFSHORE DEVELOPMENTSOCIAL MEDIA MARKETING
    TECHNOLOGIESCOLDFUSION DEVELOPMENTREACTJS DEVELOPMENTVUEJS DEVELOPMENTANGULAR DEVELOPMENTNODEJS DEVELOPMENTPYTHON DEVELOPMENTGOLANG DEVELOPMENTGOLANG CORPORATE TRAINING
    COLDFUSION EXPERTISEMURACMS EXPERTISECOLDFUSION APP MIGRATIONCOLDFUSION HOSTING & SUPPORTCF LEGACY APP MAINTENANCECOLDFUSION REST API ECOMMERCE / SHOPPING CART SOLUTIONSPRESIDECMS EXPERTISE
    MOBILE DEVELOPMENTREACT NATIVE DEVELOPMENTFLUTTER DEVELOPMENTIONIC DEVELOPMENTPHONEGAP DEVELOPMENT
  • ABOUT MITRAHSOFTOUR CLIENTSTESTIMONIALSPORTFOLIOCAREERS
  • CONTACT US
  • BLOG

ColdFusion Web Scraping Aka HTML Parsing Using JSOUP

HomeBlogColdFusion Web Scraping Aka HTML Parsing Using JSOUP
ColdFusionJSOUP

ColdFusion Web Scraping Aka HTML Parsing Using JSOUP

M
Post by MitrahsoftPublished: Oct 6, 2018

In this blog post, we are going to illustrate how to configure and extract HTML content using JSOUP in ColdFusion. JSOUP is a Java based library to work with HTML based content. It provides a very convenient API to extract and manipulate HTML content, using the best of DOM, CSS, and jquery-like selector methods.

if you want to access data from third party applications, reliable way is API access. But if original application provider don't provide any API / SOAP access to us, then we don't have any other option except Web scraping aka HTML Parsing. ColdFusion provided handy cfhttp tag, that will be enough to fetch web site content. Consider there is a list of content in a page which having user details in table format along with web page's header & footer content. scraping only all user information from the whole web page HTML content using string manipulation functions / regular expressions is tedious & time consuming task. There is a neat & easy solution for scraping the data available, that is JSOUP (Java based library - JAR ). Using this JSOUP jar, we can easily traverse, fetch & manipulate particular HTML data from the whole web page content as per our needs.

Local Environment Setup

We are currently using ColdFusion 2016 which is having Java version 1.8.0_72

  • Step 1 : Verify Java Version
  • Step 2 : Download JSOUP Archive

Step 1 : Verify Java Version

Latest JSOUP jar required java 1.5 or above. So check your JAVA version in ColdFusion admin -> "Settings Summary" tab and confirm whether the version is above 1.5 or else you need to update your JAVA version.

Step 2 : Download JSOUP Archive

Download the latest version of JSOUP jar file from repo, MVN-Repository

Overview

Using JSOUP, we can able to parse HTML content from any web site as per our needs. Here we're going to show a simple demo of parsing top 5 populated countries & that particular country's capital city information from wikipedia web site. List of countries by population page have all Countries and areas ranked by population in a table format, but this page doesn't have the capital city information. So while parsing, we should get the particular country link. Then we have to fetch that country page HTML content & scrape the capital of that country from that child page. This is commonly called as crawling or spidering the web site pages from one page to another.Cold Fusion HTML Parsing Using Jsoup ContentAbove is the partial screen shot of parent page which have countries' population information. Capital city information will be available in individual country wiki page, that link available in "Country or area" column in this table. For example, for country India, capital details will be there in Indialink. Like this way, we can able to parse N number of nested pages also and then scrape the needed content from those child pages.

Cold Fusion Web Scraping Using Jsoup Child Content

Application files Structure

My simple application files structure look like this,

  • Application.cfc : Just normal Application.cfc file which having this.javaSettings to load the JSOUP jar file
  • index.cfm : Having code to fetching web page content using jSoup & executes the parsing operation
  • jsoup-1.8.3.jar : The downloaded JSOUP jar file

Jsoup Folder Structure

jSoup selectors & DOM methods

jSoup provides sufficient enough selectors to find or manipulate elements using a CSS or jQuery-like selector syntax. As well as, it provides DOM methods to navigate a document to extract and manipulate that document data. In our example, we used various jSoup DOM methods like text(), nextElementSibling(), attr()..etc to extract data from the HTML. As well as, used different selectors like th:contains(), table.geography - Class selectors to find particular html element from the document.

Demo files

Application.cfc

cfml
component {
	this.name = "Demo_JSOUP_Scrape";

	//Loads the JAR File
	this.javaSettings = {
		loadPaths = [ "#expandPath('./jsoup-1.8.3.jar')#" ],
		reloadOnChange = false
	};
}

index.cfm

cfml
<html>
<head>
	<title>ColdFusion HTML Parsing using jsoup Demo</title>
	<style>table, th, td { border: 1px solid black; }</style>
</head>
<body>
	<cfoutput>
		<!--- Create JSOUP Object --->
		<cfset getJsoup = createObject("java", "org.jsoup.Jsoup")> 

		<!--- Load the content from the link --->
		<cfset parentURL = 'https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations)'>
		<cfset getCurrentPageContent = getJsoup.connect(parentURL).get()>

		<!--- Load the body content from the full page --->
		<cfset getBodyContent = getCurrentPageContent.body()> 

		<!--- Load the table "Countries and areas ranked by population" content from the age --->
		<cfset getTableContent = getBodyContent.select('table')[2].select('tbody')[1].select('tr')>

		<!--- Get the table header --->
		<cfset tableHeader = getTableContent.select('th')> 

		
		<!--- Display the top 30 selected number of records including header record --->
		<cfset tableDataCount = 32> 

		<!--- Get title of the web page --->
		<h2>Site Title : #getCurrentPageContent.title()#</h2> 
		<!--- Get URL location of the web page --->
		<h2>Site Location : #getCurrentPageContent.location()#</h2> 

		<table>
			<thead>
				<tr>
					<!--- Start looping the table header --->
					<cfloop array="#tableHeader#" index="j">
						<th>#j.text()#</th>
					</cfloop>
					<th>Capital</th>
				</tr>
			</thead>
			<tbody>
			<cfloop index="i" from="2" to="#tableDataCount#">
				<!--- Get the current table row from the whole content --->
				<cfset currentRowDetail = getTableContent[i].select('td')> 
				<tr>
				<!--- Loop the table current row to fetch the table data  --->
				<cfloop array="#currentRowDetail#" index="k">
					<td>#k.text()#</td>
				</cfloop>
				<cftry>
					<cfset anchorTagToGetCapital = 'https://en.wikipedia.org'&curren;tRowDetail[2].select('a')[1].attr('href')>
					<!--- Load the particular country page & fetch the capital name for that country page 
                    ( For eg: https://en.wikipedia.org/wiki/India ) --->
					<cfset countryPageHTML = getJsoup.connect(anchorTagToGetCapital).get().body()> 
					<cfset capitalOfCountry = countryPageHTML.select('table.geography th:contains(Capital)')[1].nextElementSibling()>

					<!--- From that link get the capital for that country ( eg. New Delhi ) --->
					<td>#capitalOfCountry.select('a')[1].text()#</td> 
					<cfcatch type="any">
						<td></td>
					</cfcatch>
				</cftry>
				</tr>
			</cfloop>
			</tbody>
		</table>
	</cfoutput>
</body>
</html>

Code Details

  • getJsoup = createObject("java", "org.jsoup.Jsoup") : Create JAVA object to refer JSOUP.
  • getCurrentPageContent = getJsoup.connect().get() : Fetch the content of link provided. Similar as cfhttp
  • getCurrentPageContent.title() : It gives the page's HTML title.
  • getCurrentPageContent.body() : It gives the page's body content which we will be parsing.
  • getCurrentPageContent.body().select().text() : Using this we can use selectors to fetch the required content from the web page's body content.

Result

While running the application, we get result like this, which display the country details as per the link List of countries by population including the Capital city(scrape inside of each country's link) as additional column.
Cold Fusion HTML Parsing Using Jsoup Result

Tags

ADYENAMAZON-SESANDROIDAPACHEAPACHE-JMETERAWSBARCODE-SCANNERCOLDBOXCOLDFUSIONCOLDFUSION-API-INTEGRATIONCOLDFUSIONBUILDERECHARTSEVENT-GATEWAYFFMPEGFW1JQUERYJSOUPLUCEEMURACMSMURACMS-THEMENODEJSONESIGNALOSSPAYFLOW-PROPAYMENT-GATEWAYPAYPALPRESIDECMSPUPPETEERRAILORAZUNAREACTJSREACTNATIVERESTSASS-COMPILATIONSEMANTIC-UISUBLIMETEXTTINYMCETWILIOYUI-LIBRARY

Archives

JAN 20DEC 19AUG 19JUL 19JUN 19MAY 19APR 19MAR 19FEB 19JAN 19DEC 18NOV 18OCT 18MAR 16NOV 15SEP 15MAY 15MAY 14OCT 13JUN 13MAY 13APR 13MAR 13FEB 13JAN 13

Follow us

Mitrahsoft
United StatesUnited States

Hurst, Texas, United States

+1 (817) 606-8684usa-sales@mitrahsoft.com

IndiaIndia

Head Office

126G2/5, Thiru malai nagaram,
Kovilpatti - 628 501

Madurai

1/B, 2nd & 3rd floor, GV Complex,

Bye Pass Road, SS Colony,

Madurai - 625 016

Coimbatore

2nd Floor, Sri Narmadha Towers,

Murugan Nagar Rd, Thoppampatti Pirivu,

 K. Vadamadurai, Coimbatore - 641 017

+91 9092480924

business@mitrahsoft.com

ColdFusion
  • eCommerce / Shopping Cart Solutions
  • Payment Gateway Integrations
  • Shipping API Integrations
  • ColdFusion Development Services
  • REST API Development
  • MuraCMS Plugin Creation & Personalization
Technology Services
  • ReactJS Development
  • VueJS Development
  • AngularJS Development
  • NodeJS API Development
  • Python Development
  • Golang Development
  • React Native Development
  • D3.JS data Visualization
Other Services
  • Software Development
  • Mobile Development
  • Blockchain Development
  • Cloud & Devops Services

Connect with MitrahSoft

Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field
Please fill out this field

© 2026 All Rights Reserved. MitrahSoft Solutions Pvt Ltd

Home|About Us|Careers