文章目录

  • 什么是 Python 中的分页
  • 带有下一个按钮的 Python 分页
  • 没有下一个按钮的 Python 分页
  • 无限滚动的 Python 分页
  • 带有加载更多按钮的分页



在本文中,我们将了解分页以及如何克服 Python 中与分页相关的问题。 读完本文后,我们将能够了解 Python 分页以及如何使用它处理问题。


什么是 Python 中的分页

使用任何 Web 应用程序时,最重要的是显示的内容不仅限于并强制适合单个页面。 不过,它应该显示在多个页面上,这有助于获得更好的用户体验。

这种将内容分布在多个页面上的过程称为分页。 必须记住,在实施分页概念时,我们应考虑总页数、内容类型、所讨论主题的分类表示以及页面所遵循的数字顺序等因素。


带有下一个按钮的 Python 分页

分页并不总是限于用户看到的内容,即网站的前端,但有时对后端使用的 API 进行分页也很重要。 我们可以使用几个 Python API 和模块来处理分页问题。

我们将从使用请求模块开始。 此外,如果我们有兴趣从网页中查找内容,我们将使用 BeautifulSoup4。

此外,我们将使用 lxml 库来方便地访问上述模块。

示例代码:

pip install requests beautifulsoup4 lxml

上面的行将帮助我们通过 beautifulsoup4 库安装请求模块。

import requests
from bs4 import BeautifulSoup
findurl = 'http://books.toscrape.com/catalogue/category/books/fantasy_19/index.html'
getresponse = requests.get(findurl)
getsoup = BeautifulSoup(getresponse.text, "lxml")
footer_element = getsoup.select_one('li.current')
print(footer_element.text.strip())

输出:

Page 1 of 3

前面的代码片段将帮助我们从代码中给出的网页 URL 中捕获页脚。 您可以根据需要更改 URL。

请求库在 URL 上发送一个获取请求。

对于 soup 对象,我们使用 CSS 选择器。 例如,如果我们想移动到另一个元素,我们可以在 soup.select_one(name) 中输入名称。

上面的代码适用于包含下一步导航按钮的网页。 除了这种情况,对于使用无限滚动和加载更多按钮的网站,也可以在没有下一个按钮的情况下完成分页。

python flask 数据库分页 python分页功能_分页


没有下一个按钮的 Python 分页

一些网站使用 1、2、3、4 等数字在不同页面之间滚动,而不是下一步按钮。 这使用户更容易在多个页面之间导航。

在这种情况下,我们将尝试从第一页检索数据,然后使用循环进行导航。

示例代码:

# Handling pages with the Next button
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
def process_pages():
    get_url = 'https://www.test.com/doc/791526.Zaloz-zbroje'
    response = requests.get(get_url)
    soup = BeautifulSoup(response.text, 'lxml')
    page_link_el = soup.select('.pgr_nrs a')
    # process the first page
    for link_el in page_link_el:
        link = urljoin(get_url, link_el.get('href'))
        response = requests.get(link)
        soup = BeautifulSoup(response.text, 'lxml')
        print(response.url)
        # process remaining pages
if __name__ == '__main__':
    process_pages()

输出:

https://www.test.com/doc/791526.Zaloz-zbroje/2
https://www.test.com/doc/791526.Zaloz-zbroje/3
https://www.test.com/doc/791526.Zaloz-zbroje/4

无限滚动的 Python 分页

顾名思义,在这种类型的分页中,我们没有下一个按钮或页码,而是不断滚动以查看所需的内容。

这种分页的一个简单示例可以是任何电子商务网站。 我们一次显示一定数量的产品,向下滚动后,我们将显示下一个产品。

必须记住,在这种情况下,我们不必处理多页 URL。

对 API 的异步调用将帮助我们在移动时获得更多内容。


带有加载更多按钮的分页

这种分页方法类似于无限滚动方法,但只有当我们想知道如何移动到下一页时才会有所不同。

在这种情况下,我们有一定数量的请求要完成,只要我们点击加载更多按钮,这些请求就会不断减少。 例如,网站上的图片总数为 500,我们一次显示 30 张图片。

因此,每次单击“加载更多”按钮时,我们都会看到接下来的 30 张图像,并且计数器会从总共 500 张图像中减去这 30 张。 让我们考虑下面的示例以便更好地理解。

示例代码:

import requests
from bs4 import BeautifulSoup
url = 'http://test.org/wp-json/smthstapi/v1/objects?tag=938&page={}'
null=0
page_counter = 1
while True:
    getresponse = requests.get(url.format(page_counter), headers=null)
    data = getresponse.json()
    # Process data
    # ...
    print(getresponse.url)  # only for debug
    if data.get('remaining') and int(data.get('remaining')) > 0:
        page_counter += 1
    else:
            break

输出:

https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=1
https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=2
https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=3
https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=4
https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=5
https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=6
https://test.org/wp-json/smthstapi/v1/objects?tag=938&page=7
...

上面的代码将继续打印相同的 URL 并递增页码,直到我们到达可用页面的末尾。 对于上面的代码,总页数是 34。

我们希望您发现本文有助于理解 Python 中分页的概念。