我正在尝试解析网站 dl-protect 并给出此类型的 url:http://www.dl-protect.com/F469D615例如,输出将直接是 uptobox 链接。
我尝试使用 Chrome 开发控制台弄清楚该服务是如何工作的。
首先,有两种情况需要考虑:
您不需要输入验证码,只需单击继续按钮即可。然后NodeJs程序应该返回在第二页上找到的URL(这里是uptobox)
您需要输入验证码。在这种情况下,NodeJs 程序应返回验证码的 URL
到目前为止,这是我的代码(用 ES6 编写):
import request from 'request';
import cheerio from 'cheerio';
// try to respect the header has if it were coming from a browser
let options = {
url: 'http://www.dl-protect.com/F469D615',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'fr,en-US;q=0.8,en;q=0.6,fr-FR;q=0.4',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'www.dl-protect.com',
'Origin': 'http://www.dl-protect.com',
'Referer': 'http://www.dl-protect.com/F469D615',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36'
}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
// parse the body response with cheerio
let $ = cheerio.load(body);
// detect if a captcha is required
let isCaptcha = !!$('#captcha').length;
// url of the captcha if needed
let captchaUrl = '';
// display wether we need captcha or not
switch (isCaptcha) {
case true:
captchaUrl = $('#captcha').attr('src');
console.log(`Captcha required, URL : ${captchaUrl}`);
break;
case false:
console.log('No captcha required');
break;
}
// get the key
let formKey = $('form[name="ccerure"] input[name="key"]').attr('value');
console.log(`key : ${formKey}`);
// set the form as it's computed no need to get it
// this param is just data about the browser so I ended up copying it once it was generated
let formIn = [
'_UETCF0UJREfkVmbpZWZk5Wd7QXYtJ3bGBCduVWb1N2bEBSZsJWY0J3bQtj',
'cldXZpZXLmRGctwWYuJXZ05Wa7IXZ3VWaWBiREBFItVXat9mcoNkJkVmbpZ',
'WZk5Wd74CduVGdu92Yg8WZklmdv8WakVXYgwUTUhEIm9GIrNWYilXYsBHIy',
'9mZgMXZz5WZjlGbgUmbpZXZkl2VgMXZsJWYuV0OvNnLyVGdwFGZh1GZjVmb',
'pZXZkl2dilGb7UGb1R2bNBibvlGdwlncjVGRgQnblRnbvNEIl5Wa2VGZpdl',
'JkVmbpZWZk5Wd7sTahpGall2ZmV2bo9mZvp2blFGciJmamN2Zk1mYmpGatt',
'jcldXZpZFIGREUg0Wdp12byh2Q8ZzMuczM18SayFmZhNFI4ATMuMjM2IjLw',
'4SO08SZt9mcoNEI4ATMuMjM2IjLw4SO08Sb1lWbvJHaDBSd05WdiVFIp82a',
'jV2RgU2apxGIswUTUh0SoAiNz4yNzUzL0l2SiV2VlxGcwFEIpQjNfZDO4BC',
'e15WaMByOxEDWoACMuUzLhxGbpp3bNxHNygHN0YDewMTN=='
].join('');
// if no captcha
if (!isCaptcha) {
// override the initial options by adding the necessary form data
options = Object.assign({}, options, {form: {key: formKey, i: formIn, submitform: 'Continuer'}});
// reach the same page with a post containing the following data : key, i and submitform
request.post(options, function (error, response, body) {
console.log(body);
// console.log(response);
// console.log(error);
});
}
}
});
当我查看 Chrome 开发面板(网络选项卡 + 保留日志)时,只要我单击“继续”按钮,它就会显示以下内容:
我真的认为传递“key”、“i”和“submitform”就足够了,但事实并非如此。它只是返回到第一页,而不是通过 URL 转到第二页。
任何有关如何获取 uptobox 链接(在本例中)作为输出的线索都会非常好。
谢谢!
最佳答案
大多数网站都会尽力保护自己免受人们抓取其网站的侵害——他们的原因是谨慎的,原因将是他们自己的——但是,保护网站的典型方法是使用 cookie 和隐藏字段等,每一个都经过签名并且带有时间戳并且已过期,甚至可能在后端验证为单次使用。
这个网站具体做什么是任何人的猜测,也是他们内部安全工程的一部分。
因此,您可能无法像您尝试做的那样进行简单的爬行,并且您将需要一个完整的浏览器来完成这项工作 - 幸运的是(对您来说)有 headless 浏览器,例如 PhantomJs,它可能是帮助。
关于javascript - 使用 NodeJs 解析网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37284470/