gpt4 book ai didi

javascript - 使用 Cheerio 抓取网页

转载 作者:行者123 更新时间:2023-12-03 07:56:26 28 4
gpt4 key购买 nike

我正在尝试使用 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);
}
});
});

这在发布请求上失败了,我不确定是否是因为我执行了错误的回调,或者发布请求是否不是解决机器人问题的有效方法。非常感谢任何帮助,谢谢。

最佳答案

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

关于javascript - 使用 Cheerio 抓取网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778223/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com