Site icon Vinsguru

JMeter – How To Do Web Scraping

Overview:

We have millions of web sites! They all offer tons of data which can be viewed only by a web browser.  Most of the sites do not provide any mechanism to save the data in your local machine we are interested in. Web Scraping is a process of extracting data from web site based on the given requirement.

Automation engineers know how to automate the process of launching a website and extracting certain information from a site based on a given requirement. Automating a browser is not a robust way of extracting the data!

There are commercial tools for this purpose! In this short article, I would like to show you how we could use JMeter for FREE for web scraping.

Sample Requirement:

Lets assume that We need to collect the price of given stocks from google finance website. Write the price in a text file every minute.

Steps to follow:

Some people use Selenium for this requirement because it is one of the browser automation tools!

But, We are going to use JMeter for the following reasons!

Why JMeter:

Lets start with JMeter script for the above requirement.

Scripting In JMeter:

jmeter.test.home=${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
result=

 

result = vars.get("result") + vars.get("reg.price").replace(",", "") + ",";
log.info(result);
vars.put("result", result);
def file = new File(vars.get("jmeter.test.home") + "result.csv");
file << vars.get("result") + "\n";
vars.put("result","");

 

That is it! Run the JMeter test with specific duration (update in the Thread Group settings). JMeter will produce the result like this!

 

Instead of writing this  data in CSV file, We could easily write this in Influxdb (as shown in this article) – create a Grafana chart as shown here.

 

Happy scraping and Subscribe 🙂

 

 

Share This:

Exit mobile version