码迷,mamicode.com
首页 > 其他好文 > 详细

chrome插件:提取页面数据

时间:2015-05-26 15:40:06      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

近期在做chrome插件,需要提取页面里的一些数,简单说就是企业版微博的后台数据,因为微博不提供导出功能。最开始是用php完成的,但是每次都需要先把页面代码保存下来,再进行提取,很麻烦。chrome插件可以和页面DOM交互,看起来很方便,所以开始学着写。javascript和jQuery都是边学习边写。

以下代码实现了把需要的数据提出来,需要补充xmlHttpObject部分。

<script type="text/javascript" charset="UTF-8" src="http://chenbinx.cn/kaiguan/jquery-2.0.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("span.main_title").click(function(){
        console.log("执行了!");
        var m=$("[class=‘S_line2 W_tc fst‘]");
        time=m[0];//选取第一个节点  
        console.log("日期为:"+time.innerText);//why
        var content=nextSibling(time);//获取兄弟节点  
        console.log("博文内容为:"+content.innerText);
        var read=nextSibling(content);
        console.log("博文阅读数为:"+read.innerText);
        var zan=getFirst(nextSibling(read));//获取下一个兄弟的第一个孩子节点 
        console.log("转评赞为:"+zan.innerText);
        var clicknum=nextSibling(zan);//获取这个孩子的下一个兄弟  
        console.log("短链点击为:"+clicknum.innerText);
    })
})

function getFirst(elem){  
    for(var i=0,e;e=elem.childNodes[i++];){  
        if(e.nodeType==1)  
            return e;  
    }         
}  

        //下一个兄弟节点
        function nextSibling(node) {
            var tempLast = node.parentNode.lastChild;
            if (node == tempLast) return null;
            var tempObj = node.nextSibling;
            while (tempObj.nodeType != 1 && tempObj.nextSibling != null) {
                tempObj = tempObj.nextSibling;
            }
            return (tempObj.nodeType==1)? tempObj:null;
        }
        //前一个兄弟节点
        function prevSibling(node) {
            var tempFirst = node.parentNode.firstChild;
            if (node == tempFirst) return null;
            var tempObj = node.previousSibling;
            while (tempObj.nodeType != 1 && tempObj.previousSibling != null) {
                tempObj = tempObj.previousSibling;
            }
            return (tempObj.nodeType==1)? tempObj:null;
        }    
</script>

 这里把需要提取数的源代码也附上

<body>
<div class="E_PCD_chart2">
    <div class="WB_cardtitle_b S_line2">
      <h4 class="obj_name">
        <span class="main_title W_fb W_f14">博文分析</span>
        <span class="subtitle S_txt2">
          <span class="W_icon icon_askS" action-type="layerIcon" action-data="t=2"></span>
        </span>
      </h4>
          </div>
        <div class="WB_innerwrap" node-type="articlesList">
              <table class="EM_table">
          <colgroup>
            <col width="12%">
            <col width="37%">
            <col width="12%">
            <col>
            <col width="9%">
          </colgroup>
          <thead>
            <tr>
              <th class="S_line2 S_txt2 W_tc fst">发布时间</th>
              <th class="S_line2 S_txt2">博文内容</th>
              <th class="S_line2 S_txt2">博文阅读数</th>
              <th class="S_line2 S_txt2 W_tc">
                <span>互动数</span>
                <i class="ann c1"></i>
                <span>转评赞</span>
                <i class="ann c2"></i>
                <span>短链点击</span>
              </th>
              <th class="S_line2 S_txt2 lst">
                操作
                <span class="W_icon icon_askS" action-type="layerIcon" action-data="t=1"></span>
              </th>
            </tr>
          </thead>
          <tbody>
                          <tr>
                <td class="S_line2 W_tc fst">
                  <p>2015-05-24 21:39</p>
                </td>
                <td class="S_line2">
                  <a href="http://weibo.com/1854902371/CjmjFoKBD" target="_blank" class="S_txt1">#520爱尚我# 呛口同款西班牙芭蕾新履,Pretty Ballerinas大促5折...</a>
                </td>
                <td class="S_line2">7564</td>
                <td class="S_line2">
                  <dl class="EM_stripe clearfix">
                    <dt class="W_fl W_tr">6</dt>
                    <dd class="W_fl">
                      <span class="stripe">
                        <i class="inner c1" style="width:2.52%;"></i>
                      </span>
                    </dd>
                  </dl>
                  <dl class="EM_stripe clearfix">
                    <dt class="W_fl W_tr">12</dt>
                    <dd class="W_fl">
                      <span class="stripe">
                        <i class="inner c2" style="width:5.04%;"></i>
                      </span>
                    </dd>
                  </dl>
                </td>
                <td class="S_line2 lst">
                                      <a class="S_txt1" href="javascript:;" action-type="extension" action-data="url=http%3A%2F%2Fpromote.vip.weibo.com%2Fpromote%3Ffrom%3De_weibo%26mid%3D3846140195899029%26ru%3Dhttp%3A%2F%2Fweibo.com%2Fp%2F1006061854902371%2Fmanage%3Fiframe_url%3Dhttp%3A%2F%2Fe.weibo.com%2Fv1%2Feps%2Fdata%2Farticlestats%26failRu%3Dhttp%3A%2F%2Fweibo.com%2Fp%2F1006061854902371%2Fmanage%3Fiframe_url%3Dhttp%3A%2F%2Fe.weibo.com%2Fv1%2Feps%2Fdata%2Farticlestats" suda-uatrack="key=DataService&amp;value=123">推广</a>
                                  </td>
              </tr>
                          <tr>
                <td class="S_line2 W_tc fst">
                  <p>2015-05-24 17:26</p>
                </td>
                <td class="S_line2">
                  <a href="http://weibo.com/1854902371/CjkEYrqia" target="_blank" class="S_txt1">#520爱尚我# 尚品网5周年大促即将结束,ASH全场4折起!明星款至...</a>
                </td>
                <td class="S_line2">16204</td>
                <td class="S_line2">
                  <dl class="EM_stripe clearfix">
                    <dt class="W_fl W_tr">6</dt>
                    <dd class="W_fl">
                      <span class="stripe">
                        <i class="inner c1" style="width:2.52%;"></i>
                      </span>
                    </dd>
                  </dl>
                  <dl class="EM_stripe clearfix">
                    <dt class="W_fl W_tr">17</dt>
                    <dd class="W_fl">
                      <span class="stripe">
                        <i class="inner c2" style="width:7.14%;"></i>
                      </span>
                    </dd>
                  </dl>
                </td>
                <td class="S_line2 lst">
                                      <a class="S_txt1" href="javascript:;" action-type="extension" action-data="url=http%3A%2F%2Fpromote.vip.weibo.com%2Fpromote%3Ffrom%3De_weibo%26mid%3D3846076526535926%26ru%3Dhttp%3A%2F%2Fweibo.com%2Fp%2F1006061854902371%2Fmanage%3Fiframe_url%3Dhttp%3A%2F%2Fe.weibo.com%2Fv1%2Feps%2Fdata%2Farticlestats%26failRu%3Dhttp%3A%2F%2Fweibo.com%2Fp%2F1006061854902371%2Fmanage%3Fiframe_url%3Dhttp%3A%2F%2Fe.weibo.com%2Fv1%2Feps%2Fdata%2Farticlestats" suda-uatrack="key=DataService&amp;value=123">推广</a>
                                  </td>
              </tr>
                          <tr>
                <td class="S_line2 W_tc fst">
                  <p>2015-05-24 14:05</p>
                </td>
                <td class="S_line2">
                  <a href="http://weibo.com/1854902371/CjjlobuW8" target="_blank" class="S_txt1">#520爱尚我# 尚品网5周年大促,URBAN REVIVO、DAZZLE、d‘zzit等...</a>
                </td>
                <td class="S_line2">13614</td>
                <td class="S_line2">
                  <dl class="EM_stripe clearfix">
                    <dt class="W_fl W_tr">3</dt>
                    <dd class="W_fl">
                      <span class="stripe">
                        <i class="inner c1" style="width:1.26%;"></i>
                      </span>
                    </dd>
                  </dl>
                  <dl class="EM_stripe clearfix">
                    <dt class="W_fl W_tr">16</dt>
                    <dd class="W_fl">
                      <span class="stripe">
                        <i class="inner c2" style="width:6.72%;"></i>
                      </span>
                    </dd>
                  </dl>
                </td>
                <td class="S_line2 lst">
                                      <a class="S_txt1" href="javascript:;" action-type="extension" action-data="url=http%3A%2F%2Fpromote.vip.weibo.com%2Fpromote%3Ffrom%3De_weibo%26mid%3D3846025942740532%26ru%3Dhttp%3A%2F%2Fweibo.com%2Fp%2F1006061854902371%2Fmanage%3Fiframe_url%3Dhttp%3A%2F%2Fe.weibo.com%2Fv1%2Feps%2Fdata%2Farticlestats%26failRu%3Dhttp%3A%2F%2Fweibo.com%2Fp%2F1006061854902371%2Fmanage%3Fiframe_url%3Dhttp%3A%2F%2Fe.weibo.com%2Fv1%2Feps%2Fdata%2Farticlestats" suda-uatrack="key=DataService&amp;value=123">推广</a>
                                  </td>
              </tr>
                      </tbody>
        </table>
</div>
</div>
</body>

 

chrome插件:提取页面数据

标签:

原文地址:http://www.cnblogs.com/cibirii/p/4530578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!