您的位置:首页 > 编程语言 > Lua

为儿子取高分名字而编写的lua程序

2013-12-19 09:17 288 查看
11月为给儿子取名字,折腾了2个星期,我们家大事都需要老婆做主,取名字当然算是大事,虽然我想好些名字,但都被否决,老婆需要算五行,名字的评分要好高,找了个神棍网站,我也抱着宁可信其有的态度,名字要好听,得分要高,还要有含义,简直就是大海捞针,折腾两个星期后,名字终于取出来,为此还写了程序来捞这个针,虽然最后并不是从中定,但也是记录养孩子的不容易.

用luacurl实现在网上名字评分,为儿子起名

1.网页抓取

   发现抓不,通过firebug比较

   需要加上c:setopt( curl.OPT_HTTPHEADER, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0" )

2.urlencode utf-8编码

   lua以前只实现ascii编码,utf-8还没找到解决方法,决定用cli调c#来编码名字

       require  "kpBaseLuaClrApi"

       local nameutf8 = csharpUrlEncode(name)

3.正则表达式找出关键的分数

      local pattern = "<div class=\"qtotal\"><span>(?<v1>.*?)

?<v2>.*?)<"

      local restable,patcount = csharpRegex(html,pattern);

4.读文件字典

     file = io.open("namedic.txt","r")

local count = 0

local xuancount = 1

for line in file:lines() do

5.字符转数字

   local fen = restable[1].v2

    if tonumber(fen)>=95 then

local myLuaBasePath = "D:\\kpServices\\luapublicApi"

package.path = package.path..";"..myLuaBasePath.."\\?.lua;"

require  "luafile_SysApi"

require  "kpBaseLuaClrApi"

curl = require("luacurl")

function get_html(url, c)

    local result = { }

    if c == nil then

        c = curl.new()

    end

    c:setopt(curl.OPT_URL, url)

        c:setopt( curl.OPT_HTTPHEADER, "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0" )

    c:setopt(curl.OPT_WRITEDATA, result)

    c:setopt(curl.OPT_WRITEFUNCTION, function(tab, buffer)     --call back函数,必须有

        table.insert(tab, buffer)                      --tab参数即为result,参考http://luacurl.luaforge.net/

        return #buffer

    end)

    local ok = c:perform()

    return ok, table.concat(result)             --此table非上一个table,作用域不同

end

file = io.open("namedic.txt","r")

local count = 0

local xuancount = 1

for line in file:lines() do

        count = count + 1

        if count>11315 then

                local name = '王'..line

                local nameutf8 = csharpUrlEncode(name)

                local url = "http://www.123cha.com/xm/"..nameutf8.."-0"

                local ok, html = get_html("http://www.123cha.com/xm/"..nameutf8.."-0")

                if ok then

                  local pattern = "<div class=\"qtotal\"><span>(?<v1>.*?)

?<v2>.*?)<"

                  local restable,patcount = csharpRegex(html,pattern);

                  if patcount>0 then

                          local fen = restable[1].v2

                          if tonumber(fen)>=95 then

                                print(xuancount..','..name..','..restable[1].v2..'------------'..count)

                                xuancount = xuancount + 1

                          end

                  end

                else

                        print ("Error" )

                end

        end ---ifend

        --print(count..','..line)

        --if count>1000 then

          --break

        --end

end

-----------------luafile_WebApi.lua------------------------------------------------------------------------

--local myLuaBasePath = "D:\\work\\lua\\public"

--package.cpath = myLuaBasePath.."\\kpluacurl.dll;"..package.cpath

--package.path = ".;"..package.path

--package.cpath = ".\\luacurl.dll;"

--package.path = ".;"

--print("--"..package.cpath)

require "kpluacurl"

local function curlWrite( bufferTable )

 return function( stream, buffer )

  table.insert( bufferTable, buffer )

  return string.len( buffer )

 end

end

function luaweb_urlget( URL, session )

 local ht = {}

 local t = {}

 local curlObj = curl.new()

 curlObj:setopt( curl.OPT_HEADER, true )

 curlObj:setopt( curl.OPT_HEADERFUNCTION, curlWrite( ht ) )

 curlObj:setopt( curl.OPT_WRITEFUNCTION, curlWrite( t ) )

 curlObj:setopt(curl.OPT_ENCODING, "gb2312")

 curlObj:setopt( curl.OPT_URL, URL )

 if ( session ) then

  curlObj:setopt( curl.OPT_COOKIE, session )

 end

 curlObj:perform()

 curlObj:close()

 local hr = table.concat( ht, "" )

 local r = table.concat( t, "" )

 local h, c, m = string.match( r, "(.-) (.-) (.-)\n" )

 t = {}

 for cookie in string.gmatch( hr, "Set%-Cookie: (.-);" ) do

  table.insert( t, cookie )

 end

 local cookie = table.concat( t, "; " )

 return r, tonumber( c ), cookie

end

function luaweb_urlpost( URL, session, postData )

 local ht = {}

 local t = {}

 local curlObj = curl.new()

 curlObj:setopt( curl.OPT_HEADER, true )

 curlObj:setopt( curl.OPT_HEADERFUNCTION, curlWrite( ht ) )

 curlObj:setopt( curl.OPT_WRITEFUNCTION, curlWrite( t ) )

 curlObj:setopt( curl.OPT_URL, URL )

 curlObj:setopt( curl.OPT_POSTFIELDS, postData )

 if ( session ) then

  curlObj:setopt( curl.OPT_COOKIE, session )

 end

 curlObj:perform()

 curlObj:close()

 local hr = table.concat( ht, "" )

 local r = table.concat( t, "" )

 local h, c, m = string.match( r, "(.-) (.-) (.-)\n" )

 t = {}

 for cookie in string.gmatch( hr, "Set%-Cookie: (.-);" ) do

  table.insert( t, cookie )

 end

 local cookie = table.concat( t, "; " )

 return r, tonumber( c ), cookie

end



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: