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
- Lua解析Html简单示例。
- 使用Jsoup简单解析HTML文件示例
- lua html式解析
- 学习Lua(2) -- Lua 简单示例
- 简单HTML 示例
- LUA解析配置文件 简单实例
- 简单解析Lua的堆栈
- wireshark协议解析插件【LUA】示例
- HTML表格标记简单示例
- HTML 表格标签简单示例
- 学习Lua(2) -- Lua 简单示例 收藏
- lua学习系列:c++调用lua脚本的简单示例
- Lua解析Html的正则表达式Demo
- glib命令行解析简单示例
- android json 解析 简单示例
- Android json解析简单示例
- 【Cocos2d-x lua篇003】简单解析Lua的堆栈
- ngx lua模块源码简单解析
- vector的一般用法
- iOS study Day6-计算器2
- 转载_驅動程序的組織結構
- MIME type 类型大全
- delphi-x2d 特性和环境
- Lua解析Html简单示例。
- Oracle 还原历史统计信息
- camera摄像原理之三:色温和自动白平衡
- "name.bat" is not a recognized as an internal or external command,operable program or batch file
- samba的一些小结
- Binding上
- Test
- 测试JSTL表达式 .
- Redis安装整理(window平台) +php扩展redis