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

博主分享了如何利用Lua程序结合网络评分系统,为儿子筛选出高分且寓意良好的名字。程序涉及网页抓取、编码转换、正则表达式等技术,通过遍历字典文件并评估每个名字的得分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值