用cheerio刮网页

时间:2016-01-13 22:23:59

标签: javascript node.js web-scraping screen-scraping

我正在尝试使用cheerio抓取一个网页,但我的请求被某种机器人检测软件阻止了。响应如下:

<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>

我想我可以通过添加带有post函数的回调来解决这个问题,但它似乎不起作用。我的代码如下:

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);
  	}
  });
});

这对于post请求失败了,我不确定是不是因为我正在进行回调错误,或者post post请求不是解决bot的有效方法。非常感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

我意识到我遇到了问题因为cheerio没有加载javacsript。我试图用javacsript来抓取加载数据的网站,所以我需要使用不同的工具。可能会使用(PhantomJS)[http://phantomjs.org/]

答案 1 :(得分:0)

您应该尝试将标题设置为类似浏览器的标题。例如,您没有设置用户代理,缓存控制,接受等。这使得网站检测到您不是浏览器变得非常简单。

在正常请求中检查标题(在Chrome上使用更多工具 - >开发人员工具,网络并选择HTML文件。更多信息on this thread)并将您的内容发送到尽可能类似的地方。