Reputation: 2207
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.
Upvotes: 2
Views: 1856
Reputation: 2207
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/].
Upvotes: 2
Reputation: 80
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.
Upvotes: 0