我試圖解析網站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/)這樣的無頭瀏覽器框架。 –