2

I am trying to scrape a webpage with cheerio, but my request is being blocked with some sort of bot-detecting software. The response looks like this:

<body>
<div class="content">
  ...
  <div class="main">
    <h1>Suspicious activity detected</h1>
 <div class="box">
   <p>Due to suspicious activity from your computer, we have   blocked your access to http://www.something.com. After completing the form, your information will be evaulauted and you may be unblocked.  </p>
      <p><span>Note</span>: This website may require JavaScript. If you have JavaScript disabled, please enable it before attempting to return to http://www.something.com.</p>
    <div class="block">
  <form id="distilUnblockForm" method="post" action="http://verify.distil.it/distil_blocked.php">
 <div id="dUF_first_name">
  <label for="dUF_input_first_name">First Name:</label>
  <input type="text" id="dUF_input_first_name" name="first_name" value="" />
 </div>
 <div id="dUF_last_name">
  <label for="dUF_input_last_name">Last Name:</label>
  <input type="text" id="dUF_input_last_name" name="last_name" value="" />
 </div>
 <div id="dUF_email">
  <label for="dUF_input_email">E-mail:</label>
  <input type="text" id="dUF_input_email" name="email" value="" />
 </div>
 <div id="dUF_city" style="display: none">
  <label for="dUF_input_city">City (Leave Blank):</label>
  <input type="text" id="dUF_input_city" name="city" value="" />
 </div>
 <div id="dUF_unblock">
  <input  id="dUF_input_unblock" name="unblock" type="submit" value="Request Unblock" />
 </div>
 <div id="dUF_unblock_text">
  You reached this page when attempting to access https://someWebsite from myIPAddress on someDateInISOFormat.
 </div>
 <div id="dUF_form_fields" style="display: none">
  <input type="hidden" name="B" value="someNumbersAndLetters" />         
  <input type="hidden" name="P" value="someMoreNumbersAndLetters" />         
  <input type="hidden" name="I" value="" />         
  <input type="hidden" name="U" value="" />         
  <input type="hidden" name="V" value="###" />
  <input type="hidden" name="O" value="" />
  <input type="hidden" name="D" value="###" />
  <input type="hidden" name="A" value="###" />
  <input type="hidden" name="LOADED" value="someDate" />
  <input type="hidden" name="Q" value='someUrl' />
  <input type="hidden" id="distil_block_identity_info" name="XX" value="" />
 </div>
</form>

...

</body>

I am thinking I can get around this by added a callback with a post function, but it doesn't seem to be working. My code is below:

var url = someUrl;

request(url, function (error, response, html) {
 console.log("html", html); //where i am getting the above html
  
  request.post({
    uri: 'hhttp://verify.distil.it/distil_blocked.php',
 headers: { 'content-type': 'application/x-www-form-urlencoded' },
 body: require('querystring').stringify(credentials)
 }, function(err, res, body){
   if(err) {
   callback.call(null, new Error('Login failed'));
   return;
   } else {
        var $ = cheerio.load(html);
        var parsedResults = [];
        $('#someSelector').each(function(i, element){
          var node = $(this).children();
          // Get all the children
          var something = $(node).eq(0).text();
          var anotherThing = $(node).eq(1).text();
          var oneMoreThing = $(node).eq(2).text();
          // Make it into an object
          var metadata = {
         something: something,
         anotherThing: anotherThing,
           oneMoreThing: oneMoreThing
          };
          // Push meta-data into parsedResults array
          parsedResults.push(metadata);
        });
        // Log our finished parse results in the terminal
        console.log(parsedResults);
   }
  });
});

This is failing on the post request, I am not sure if it is because I am doing the callback wrong or if the post request is not a valid way to work around the bot. Any help is greatly appreciated, thanks.

0xPingo
  • 2,047
  • 4
  • 19
  • 43
  • Seems like it's blocking your IP (so POST wouldn't help) – Lucas Jan 13 '16 at 22:42
  • Ok. The url works in my regular browser window before, during, and after receiving that html response, so I am not sure if my IP is the issue (think it's logged, not blocked). @Lucas – 0xPingo Jan 13 '16 at 23:08

2 Answers2

2

I realized I was having issues because cheerio does not load javacsript. The site I was trying to scrape loads data with javacsript, so I need to use a different tool. Probably will use (PhantomJS)[http://phantomjs.org/].

0xPingo
  • 2,047
  • 4
  • 19
  • 43
  • How did you come to the realization that you need to run javascript? – Michael Hewson Jan 17 '16 at 23:05
  • @mikeyhew I was playing around with import.io and that gives you the option to execute javascript (or not). I noticed I was only getting redirected to that page when js was disabled. – 0xPingo Jan 19 '16 at 01:23
0

You should try setting your headers as browser-like as you can. For instance, you are not setting your user-agent, cache-control, accept, etc. That makes it dead simple for the site to detect you are not a browser.

Check the header on a normal request (on Chrome use More Tools-> Developer Tools, network and select the HTML file. More info on this thread) and send yours as similar to those as you can.

Community
  • 1
  • 1