Lua解析Html简单示例。

来源:互联网 发布:爬虫软件怎么用 编辑:程序博客网 时间:2024/05/08 03:36

这段时间项目需要

做了这个解析

只是简单的字符截取

但是有很大的作用

不多说

下面贴Lua源码

免得以后大家走弯路

local str = "四川<span style=\"color:#003399;\">成都</span><a href=\"onClick\"><span style=\"color:#003399;\">东软</span></a><span style=\"color:#003399;\">##Colege###dinghq.alex@gmail.com</span><span style=\"color:#003399;\">学院</span>"function setTagText(text,color,onClick,key)singleText = {};singleText.text = text;singleText.color = color;singleText.onClick = onClick;singleText.key = key;endfunction parseText(str,count)    local strIndex = string.sub(str, 1, 1);    local text = "";    local color = "";    local onClick = "";    local key = "";-- 每次循环之后剩下的字符()    local sss = ""    Text = {};    if strIndex == "<" then        local st = string.sub(str, 1, 2);-- 当字符串是以<a开头时        if st == "<a" then            local a1 = string.find(str, "<a");            local a2 = string.find(str, "</a>");            hrefTag = string.sub(str, a1, a2+3);sss = string.sub(str,a2+4);            local a3 = string.find(hrefTag,"href=");            local a4 = string.find(hrefTag,"\">");            onClick = string.sub(hrefTag,a3+6,a4-1);            local a5 = string.find(hrefTag,"color:");            local a6 = string.find(hrefTag,";\">");            hrefColor = string.sub(hrefTag,a5+6,a6-1);color = string.gsub(hrefColor, "#", "0x");strColor = string.gsub(hrefColor, "#", "0x");color = tonumber(strColor);local t1 = string.find(hrefTag,";\">");local t2 = string.find(hrefTag,"</span>");text = string.sub(hrefTag,t1+3,t2-1);local key1 = string.find(hrefTag, "##");local key2 = string.find(hrefTag, "###");if key1 == nil or key1 == "" then                key = nil;            else                key = string.sub(str,key1+2,key2-1);            endsetTagText(text,color,onClick,key)Text[count] = singleText;        elseif st == "<s" then            local s1 = string.find(str, "<span");            local s2 = string.find(str, "</span>");            spanText = string.sub(str, s1, s2+6);            sss = string.sub(str, s2+7);local s2 = string.find(spanText,"href=");            local s3 = string.find(spanText,"\">");            if s2 == nil or s2 == "" then                onClick = nil;            else                onClick = string.sub(spanText,s2+5,s3-2);            end            local s4 = string.find(spanText,"color:");            local s5 = string.find(spanText,";\">");            local spanColor = string.sub(spanText,s4+6,s5-1);-- 转换成0x开头的字符spanColor = string.gsub(spanColor, "#", "0x");-- 转换成数字color = tonumber(spanColor);local t1 = string.find(spanText,";\">");local t2 = string.find(spanText,"</span>");text = string.sub(spanText,t1+3,t2-1);local key1 = string.find(spanText, "##");local key2 = string.find(spanText, "###");if key1 == nil or key1 =="" then                key = nil;            else                key = string.sub(spanText,key1+2,key2-1);            end            setTagText(text,color,onClick,key)Text[count] = singleText;        end    else        local i1 = string.find(str, "<");        if i1 == nil or i1 == "" then            text = nil;        else            text = string.sub(str, 1, i1-1);            sss = string.sub(str, i1);        endsetTagText(text,color,onClick,key)Text[count] = singleText;    end return Text, sssendcount = 1local Text, sss = parseText(str,count);print(Text[1].text, Text[1].color,Text[1].onClick,Text[1].key);while sss ~= "" doText, sss = parseText(sss,count);print(Text[count].text, Text[count].color,Text[count].onClick,Text[count].key);count = count + 1;end


0 0