地摊经济如火如荼,万金油python如何加入摆摊,现在就简单的教你如何用python做个算命程序。
本次目标是大名鼎鼎的”诸葛神算“程序。完整代码公众号回复 python算命
00:本程序以练习 爬虫 python操作数据库、excel、python汉字转换笔画
0.1分析需求
百度诸葛神算的计算方法:
諸葛神數相傳為漢諸武侯所作,共有三百八十四籤,按照筆劃以大易三百八十四爻之數占出卦象,籤文句法長短不一,寓意深遠,變化無窮,判斷吉凶,相當準繩。
占卦方法:
例:報字為「求」「財」「運」起卦前必須心中想著要問的事情,然後報出三個字,第一個字的總筆劃為「百」位,第二個字的筆劃為「十」位,而第三個字的筆劃為「個」位。凡字的筆劃在九劃以內者,全數照算,如在十劃以外,只要個數(例如:23劃,只當3劃計),如果筆劃剛在十劃或二十劃俱照一劃計算。
報字次序為:報字、筆劃、百位、十位、個位、備註 (1) 求 7「求」字
在 9 劃之內,全數照算在「百位」之內。財 9「財」字亦在9劃之內,全數亦照算在「十位」內
運 12「運」字有十二劃,只要個位,所以當2劃計算
結果:求籤號碼為792。
因為諸葛神算是以大易三百八十四爻作為三百八十四籤.因此,如報字的結果多過三百八十四,便須減去三百八十四計算,直至到所得結果少於三百八十四。按舉例所示的結果是「792」,減去384尚餘408,便要再減384,得出的籤文為第24籤,「意孜孜,心戚戚,要平安,防出入。」
这么长。。。。
总结地说就是虔诚的在心里想三个汉字或者数字如果是汉字就把汉字转化成3个笔画
如果这3个数字大于384就把数字减去384后输入表中查询对应的签号、签文、解签信息等
02技术分析
1上网爬取 诸葛神算的 签号、签文、解签信息
2写程序实现计算
爬虫:
本文以https://zhuge.911cha.com/为例
分析
浏览器打开上面网站
发现里面是列出来了第几签,F12查看url全是按1、2、3、4排好,
也就是说不用写爬取url页直接抓取内用就可以。

详情页分析
我们需要的
签号在h2
签文在p[@class="mt noi f16"]
解签</h3><p>www.911cha.com解签
如图

让我们开始愉快的爬吧!
1导入模块
import pymysql import requests import re #正则表达式 from lxml import etree import os
连接数据库新
建数据库和建表
conn = pymysql.connect(host='localhost', user='root', passwd='密码', db="诸葛神算", charset='utf8')
cur = conn.cursor()
#第一次运行把注释去掉
# cursor.execute("drop table if exists major")
# cur.execute("""create database if not exists 诸葛神算""")#创建数据库
# cur.execute("""create TABLE if not exists book1(id int,签序中文 VARCHAR(40) ,签号 VARCHAR(40) ,签文 text ,解签 text) """)#创建数据库 python
# conn.commit()设置请求头
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",}
# get发送请求刚刚我们分析了所有的页面是按1、2、3.html排的
所以url https://zhuge.911cha.com/%s.html"%i
爬取网页
for i in range(338,385): url = "https://zhuge.911cha.com/%s.html"%i response = requests.get(url,headers=headers) # 将网页编码方式转换为utf-8 response.encoding = 'utf-8' # 网站源码 html = response.text
数据清洗
tree = etree.HTML(html)
qianhao = tree.xpath('//h2/text()')[0]
qianhao = qianhao.replace("诸葛神算",'')
print(qianhao)
qian = tree.xpath('//p[@class="mt noi f16"]/text()')[0]
print(qian)
title= tree.xpath('//title/text()')[0]
title = title.replace("[诸葛神算第%s签]"%i,'')
title = title.replace("解签 - 诸葛神算 - 911查询",'')
print(title)
jieqian = re.findall(r'解签</h3><p>www.911cha.com解签:.*?</div><div class="mcon bt"><div class="gray">',html,re.S)[0]
jieqian = jieqian.replace('解签</h3><p>www.911cha.com','')
jieqian = jieqian.replace('</div><div class="mcon bt"><div class="gray">','')
jieqian = jieqian.replace('<p>','\n')
jieqian = jieqian.replace('</p>','')
print(jieqian)数据入库:
cur.execute('insert book1(id,签序中文,签号,签文,解签) values("%s", "%s","%s", "%s","%s")' % ( i,qianhao, qian, title,jieqian))
conn.commit()
print("插入成功")
cur.close()
conn.close()执行完后可以见数据库有数据
此处有些小伙伴说我没安装数据库怎么办?
我们的可以把数据写入excel里面 xlwt具体去看另一篇
Python 操作 Excel
import xlwt
wb = xlwt.Workbook()
sh1 = wb.add_sheet('诸葛神算')
sh1.write(0, 0, 'id')
sh1.write(0, 1, '签序中文')
sh1.write(0, 2, '签号')
sh1.write(0, 3, '签文')
sh1.write(0, 4, '解签')#写入操作
all_sm = [i,qianhao, qian, title,jieqian]
for j in range(0,5):
sh1.write(i, j, all_sm[j])
# with open('爬虫数据算命.txt', 'a') as f:
# f.write(text)
wb.save('book1.xls')
第二步
汉字转化成笔画
kTotalStrokes
start: [13311, 19968, 63744, 131072, 173824, 177984, 178208, 194995]
end : [19893, 40917, 64045, 173782, 177972, 178205, 183969, 194998]
def get_stroke(c):
# 如果返回 0, 则也是在unicode中不存在kTotalStrokes字段
strokes = []
with open('strokes.txt', 'r') as fr:
for line in fr:
strokes.append(int(line.strip()))
unicode_ = ord(c)
if 13312 <= unicode_ <= 64045:
return strokes[unicode_-13312]
elif 131072 <= unicode_ <= 194998:
return strokes[unicode_-80338]
else:
print("c should be a CJK char, or not have stroke in unihan data.")
# can also return 0
#检验是否全是中文字符
def is_all_chinese(strs):
for _char in strs:
if not '\u4e00' <= _char <= '\u9fa5':
return False
return True#全中返回F开始算命:
# input_words = str(random.randint(0,9))+str(random.randint(0,9))+str(random.randint(0,9))
input_words = input("请输入3个汉字或一个3位数字")
print(input_words)
#防止输入其他不符合
while len(input_words) != 3 or (not(is_all_chinese(input_words) or input_words.isnumeric())):
if is_all_chinese(input_words)&len(input_words) == 3:
break
elif (input_words.isnumeric()&len(input_words) == 3):
break
else:
input_words = input("请输入3个汉字一个3位数字")
if input_words.isnumeric():
input_math = int(input_words)
# print(input_math)
#转换
elif is_all_chinese(input_words):
print(input_words[0],input_words[1],input_words[2])
input_math1=get_stroke(input_words[0])
input_math2=get_stroke(input_words[1])
input_math3=get_stroke(input_words[2])
if input_math1==10 or input_math1==20:
input_math1=1
if input_math2==10 or input_math2==20:
input_math2=1
if input_math3==10 or input_math3==20:
input_math3=1
print(input_math1%10,input_math2%10,input_math3%10)
input_math = input_math1*100+input_math2*10+input_math1
# print(input_math)
#计算
while input_math > 384:
input_math = input_math-384
print(input_math)
wb = xlrd.open_workbook("book1.xls")
sh1 = wb.sheet_by_index(0)
cols = sh1.row_values(input_math)
# print(sh1)
a = cols[1]
b = cols[2]
c = cols[3]
d = cols[4]
with open('算命日志.txt', 'a') as f:
f.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )
f.write('\t')
f.write(input_words)
f.write('\n')
f.write(str(a))
f.write('\n')
f.write(str(b))
f.write('\n')
f.write(str(c))
f.write('\n')
f.write(str(d))
f.write("\n\n--------------------------------\n")
print(cols[1])
print(cols[2])
print("签文",cols[3])
# input('输入解签')
print(cols[4])
整个程序就这样了由于程序比较简单所以不打包成exe了
获取代码公众回复python算命
现在我们可以出去摆摊了










