python 爬虫如何执行自动下一页循环加载文字

本文介绍如何使用Python爬虫进行网页自动化翻页并循环抓取内容。通过解析页面结构,获取下一页链接,并在每次请求后更新URL,实现连续抓取多个页面的数据。

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

from bs4 import BeautifulSoup
import requests
import time
from lxml import etree
import os
# 该demo执行的为如何利用bs去爬一些文字
def start():
    # 发起网络请求
    html=requests.get('http://www.baidu.com')
    #编码
    html.encoding=html.apparent_encoding
    #创建sp
    soup=BeautifulSoup(html.text,'html.parser')
    print(type(soup))
    print('打印元素')
    print(soup.prettify())
    #存储一下title 该方法没有提示直接展示
    title=soup.head.title.string
    print(title)
#     写入文本
    with open(r'C:/Users/a/Desktop/a.txt','w') as f:
       f.write(title)
    print(time.localtime())

url_2 = 'http://news.gdzjdaily.com.cn/zjxw/politics/sz_4.shtml'
def get_html_from_bs4(url):

    # response = requests.get(url,headers=data,proxies=ip).content.decode('utf-8')
    response = requests.get(url).content.decode('utf-8')
    soup = BeautifulSoup(response, 'html.parser')
    next_page = soup.select('#displaypagenum a:nth-of-type(9)')[0].get('href')
    # for i in nett
    print(next_page)
    ne
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值