phantomjs的正確打開方式(爬蟲)

以下是我學(xué)習(xí)使用phantomjs爬蟲的打怪升級之路,過程充滿艱辛,也充滿歡樂,但一路的風(fēng)景就是最大的樂趣,不是么?希望大家能get到想要的東西!

安裝phantomjs

實(shí)戰(zhàn)爬蟲

這里主要用到phantomjs的兩個模塊,分別是webserver、webpage。用這兩個模塊開發(fā)一個獨(dú)立的web服務(wù)。

設(shè)計思路

java通過http請求下發(fā)任務(wù),phantomjs webservice獲取任務(wù)后去處理,處理完以后再將結(jié)果返回給java。需要注意的是目前一個phantomjs webservice只支持10個并發(fā)。但我們可以在一臺服務(wù)器上多開幾個phantomjs webservice啟用不同的端口即可,或者可以多臺服務(wù)器做個集群,用nginx做反向代理。

phantomjs webservice server.js

phantom.outputEncoding = 'gbk';

var webserver = require("webserver");
var webPage = require("webpage");

var server = webserver.create()
  
var count = 0;

var service = server.listen(8888, function(request, response) {

    
    var url = "https://item.m.jd.com/product/";
    var skuCodeArg = request.post;
    var code = skuCodeArg.substr(8,skuCodeArg.length);
    //  退出服務(wù)
    if(code == "goDie"){
        console.log("phantomjs quit");
        response.statusCode = 200;
        response.write("phantomjs quit");
        phantom.exit();
        return false;
    }
    url = url + code + ".html";
    console.log("count  "+(++count)+"   url "+url);
    
    var page = webPage.create();
    page.settings.userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
    page.settings.loadImages = true;
    page.settings.clearMemoryCaches = true;
    
    page.clearMemoryCache();

    page.open(url, function start(status) {
        if (status == "success") {
                content = page.content;
                response.statusCode = 200;
                response.write(content);
                response.close();
                                //這個setTimeout必須要加,不然會出現(xiàn)內(nèi)存耗盡的提示,程序直接退出。
                setTimeout(function() {
                     setTimeout(function() {
                            page.close();
                     }, 1);
                }, 100);
        } else {
            response.statusCode = 500;
            response.write("error");
            response.close();
            setTimeout(function() {
                     setTimeout(function() {
                            page.close();
                     }, 1);
                }, 100);
        }
        
    });
    
});

服務(wù)處理 java

//得到html
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("skuCode", "123456"));
String html = HttpUtil.sendPost("http://localhost:8888", params);
Document doc = Jsoup.parse(html);
*
*  自己的處理邏輯.....
*
//關(guān)閉服務(wù)
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("skuCode", "goDie"));
HttpUtil.sendPost("http://localhost:8888", params);

使用jsoup解析Document得到自己想要的數(shù)據(jù)。

總結(jié)

使用phantomjs可以像解析靜態(tài)html一樣得到ajax返回的數(shù)據(jù)。單臺效率滿,可以多臺服務(wù)器做個集群。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容