欢迎访问Python教程网,我们的网址www.041b.com
python分页 – python教程 - python编程学习交流
python分页 – python教程 - python编程学习交流

python分页

admin2020-08-18 发布 python教程

这里将告诉您python分页,具体实现方法:

一个简单的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<pp:
                strbe="<a class='num' href='"+url+"&p="+str(i-1)+"'>"+str(i)+"</a>"+strbe
            i -= 1
        if pp<=1:
            strbe=""
        else:
            strbe="<a class='num' href='"+url+"&p=0'>首页</a>"+"<a class='num' href='"+url+"&p="+str(pp-2)+"'>上一页</a>"+strbe
        strmid="<span class='current'>"+str(pp)+"</span>"
        strlater=""
        pagelater=pp+beforlaterln
        i=pp
        while i < pagelater:
            if i<countpage:
                strlater+="<a class='num' href='"+url+"&p="+str(i)+"'>"+str(i+1)+"</a>"
            i += 1
        if pp>=countpage:
            d=0
        else:
            strlater+="<a class='num' href='"+url+"&p="+str(pp)+"'>下一页</a>"+"<a class='num' href='"+url+"&p="+str(countpage-1)+"'>尾页</a>"
        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<pp:
                strbe="<a class='num' href='"+url+"&p="+str(i-1)+"'>"+str(i)+"</a>"+strbe
            i -= 1
        if pp<=1:
            strbe=""
        else:
            strbe="<a class='num' href='"+url+"&p=0'>首页</a>"+"<a class='num' href='"+url+"&p="+str(pp-2)+"'>上一页</a>"+strbe
        strmid="<span class='current'>"+str(pp)+"</span>"
        strlater=""
        pagelater=pp+beforlaterln
        i=pp
        while i < pagelater:
            if i<countpage:
                strlater+="<a class='num' href='"+url+"&p="+str(i)+"'>"+str(i+1)+"</a>"
            i += 1
        if pp>=countpage:
            d=0
        else:
            strlater+="<a class='num' href='"+url+"&p="+str(pp)+"'>下一页</a>"+"<a class='num' href='"+url+"&p="+str(countpage-1)+"'>尾页</a>"
        page=strbe+strmid+strlater
    return "共"+str(countpage)+"页"+page
pages = pagestr('http://www.abc.com?',195,11,10,8)
print (pages)
共20页<a class='num' href='http://www.abc.com?&p=0'>首页</a>
<a class='num' href='http://www.abc.com?&p=10'>上一页</a>
<a class='num' href='http://www.abc.com?&p=3'>4</a>
<a class='num' href='http://www.abc.com?&p=4'>5</a>
<a class='num' href='http://www.abc.com?&p=5'>6</a>
<a class='num' href='http://www.abc.com?&p=6'>7</a>
<a class='num' href='http://www.abc.com?&p=7'>8</a>
<a class='num' href='http://www.abc.com?&p=8'>9</a>
<a class='num' href='http://www.abc.com?&p=9'>10</a>
<a class='num' href='http://www.abc.com?&p=10'>11</a>
<span class='current'>12</span>
<a class='num' href='http://www.abc.com?&p=12'>13</a>
<a class='num' href='http://www.abc.com?&p=13'>14</a>
<a class='num' href='http://www.abc.com?&p=14'>15</a>
<a class='num' href='http://www.abc.com?&p=15'>16</a>
<a class='num' href='http://www.abc.com?&p=16'>17</a>
<a class='num' href='http://www.abc.com?&p=17'>18</a>
<a class='num' href='http://www.abc.com?&p=18'>19</a>
<a class='num' href='http://www.abc.com?&p=19'>20</a>
<a class='num' href='http://www.abc.com?&p=12'>下一页</a>
<a class='num' href='http://www.abc.com?&p=19'>尾页</a>

希望对大家有帮助,谢谢!

python分页就为您介绍到这里.欢迎访问python自学网 www.041b.com

转载原创文章请注明,转载自: python教程 - python分页 (https://www.041b.com/40088.html)

留言

写下你的评论吧

近期评论