Parse HTML form response data

I need to parse the html response data but the html to json conversion methods are not working. Here is sample response data:
Looking to grab the csrftoken value from the html.

   <form action="" method="post">
        <input type='hidden' name='csrftoken' value='KX3x6gY56wVcK4O2' />
        <tr><th><label for="id_username">Username:</label></th><td><input type="text" name="username" maxlength="254" required autofocus id="id_username" /></td></tr>
    <tr><th><label for="id_password">Password:</label></th><td><input type="password" name="password" required id="id_password" /><input type="hidden" name="next" id="id_next" /></td></tr>
        <input type="submit" value="Submit" />

here you can use cheerio jQeury api

const $ = cheerio.load(responseBody);

console.log($( “input[name=‘csrftoken’]” ).val());


Thank you, that worked. Awesome.


when I execute const $ = cheerio.load(responseBody); then $ is null.
Any ideas?


Basically, you need to either pass in a selector, in order to see the HTML data in the console/anywhere else or using one of the functions of the Cheerio API like $.html().

More information about the Cheerio module can be found here - I’ve added an extract from that site below just to show one method of using Cheerio.

Using this HTML markup as an example:

<ul id="fruits">
  <li class="apple">Apple</li>
  <li class="orange">Orange</li>
  <li class="pear">Pear</li>


Cheerio’s selector implementation is nearly identical to jQuery’s, so the API is very similar.

$( selector, [context], [root] )

selector searches within the context scope which searches within the root scope. selector and context can be a string expression, DOM Element, array of DOM elements, or cheerio object. root is typically the HTML document string.

This selector method is the starting point for traversing and manipulating the document. Like jQuery, it’s the primary method for selecting elements in the document, but unlike jQuery it’s built on top of the CSSSelect library, which implements most of the Sizzle selectors.

$('.apple', '#fruits').text() 
//=> Apple 
$('ul .pear').attr('class') 
//=> pear 
//=> Orange

Just as a quick example of extracting a value from a HTML page using Cheerio:


Thanks Danny,

  1. I didn´t ask my question clear enough, I was checking for $.html
  2. I forgot the brackets, so $.html**()** works.
1 Like

Thanks, this has been a great help for me to capture individual form responses. Is there a way to dynamically iterate through a form to capture all name and values to create as variables? Or is it easier capturing the whole form and passing that into the next API call directly?

I need to do something similar but I need to extract a token and set an environment variable from the HTML from something like this:

Cheerio syntax here didn’t quite get me there, though I keep poking. Any ideas?

const $ = cheerio.load(responseBody);

is as close is I’ve gotten