我试图解析网站dl-protect并给出这种类型的网址:http://www.dl-protect.com/F469D615输出将直接为uptobox链接为例。使用NodeJs解析网站
我试图找出这个服务如何使用chrome dev控制台。
首先,有的2个案例体贴:
你并不需要输入验证码,你只需要点击继续按钮。然后NodeJs程序应返回第二页上的URL(此处为uptobox)
您需要输入验证码。在这种情况下的NodeJS程序应该返回Captcha验证码的网址
到目前为止,这是我的代码(写在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浏览器开发板(网络选项卡+保存日志),当我点击继续按钮,它表明我:
我真的以为通过“钥匙”,“i”和“submitform”就足够了但事实并非如此。它只是返回到第一页,而不是通过URL转到第二页。
关于如何获得输出uptobox链接(在这种情况下)的任何线索将是非常好的。
谢谢!
那么,真正的问题是什么? –
问题是,你有什么想法,为什么我不能达到我想要的页面。也许我不清楚,让我换个说法:)有2页。打开链接,你会看到第一个(带有继续按钮),如果你点击它,第二个页面带有受保护的链接。在我的代码中,我试图模拟它。所以基本上我想明白为什么我不能得到输出的uptobox链接? – Maxime
看看[osmosis](https://github.com/rchipka/node-osmosis)这样的抓取库或像[PhantomJs](http://phantomjs.org/)这样的无头浏览器框架。 –