tamptermonkey 脚本初步尝试

刚开始接触tamptermonkey

主要用于基于浏览器的数据采集 基本都是js基础

这里是采集表格里的内容, 构造json, 然后post发送到后台

如果加入定时刷新功能就可以随时或是按照固定或是随机时间采集数据了

测试:

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://zycg.cn/rjcg/pur_record
// @require      http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {

    var jsonO = [];
    $(".tr_even").each(function(i){                   // 遍历 tr
        var text = {};
        $(this).children('td').each(function(j){  // 遍历 tr 的各个 td
            if(j == 1){
                text ["采购单位"] = $.trim($(this).text());
            }
            if(j==2){
               text ["购买商品"] = $.trim($(this).text());
            }
            if(j==5){
               text ["供货单位"] = $.trim($(this).text());
            }
        });
        jsonO.push(text);

    });

    var jsonS = JSON.stringify(jsonO);
    console.log(jsonS);
    var url = "http://127.0.0.1:8000/views/";
    $.ajax({
        type: 'POST',
        url: url,
        data: jsonS,
        dataType: 'json'
    });
})();

猜你喜欢

转载自www.cnblogs.com/jeroen/p/10585925.html