一个简单的python分页小模块
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
#url连接,countdatas表数据总行数,currentpage第几页(0是第一页),everpage每页多少行,beforlaterln前后长度
def pagestr(url,countdatas,currentpage,everpage,beforlaterln):
page=""
intpart=countdatas/everpage
remainderpart=countdatas%everpage #余数
countpage=int(intpart) #取整数
currentpage=(0 if (currentpage<0) else currentpage)
if remainderpart==0:#余数判断
currentpage=(countpage-1 if (currentpage>countpage-1) else currentpage)
countpage;#总页数
pp=currentpage+1
strbe=""
pagebefore=pp-beforlaterln-1
i = pp
while i>pagebefore :
if i>0 and i
strbe=""+str(i)+""+strbe
i -= 1
if pp<=1:
strbe=""
else:
strmid=""+str(pp)+""
strlater=""
pagelater=pp+beforlaterln
i=pp
while i < pagelater:
if i
strlater+=""+str(i+1)+""
i += 1
if pp>=countpage:
d=0
else:
page=strbe+strmid+strlater
else:
countpage+=1 #总页数
currentpage=(countpage-1 if (currentpage>countpage-1) else currentpage)
pp=currentpage+1
strbe=""
pagebefore=pp-beforlaterln-1
i = pp
while i>pagebefore :
if i>0 and i
strbe=""+str(i)+""+strbe
i -= 1
if pp<=1:
strbe=""
else:
strmid=""+str(pp)+""
strlater=""
pagelater=pp+beforlaterln
i=pp
while i < pagelater:
if i
strlater+=""+str(i+1)+""
i += 1
if pp>=countpage:
d=0
else:
page=strbe+strmid+strlater
return "共"+str(countpage)+"页"+page
pages = pagestr('http://www.abc.com?',195,11,10,8)
print (pages)
运行后得到以下字符串
共20页首页
12
希望对大家有帮助,谢谢!