当前位置: 首页 > wzjs >正文

网站面包屑导航网络营销策划的方法

网站面包屑导航,网络营销策划的方法,免费的客户管理软件哪个好用,企业内网搭建要多少钱Python 第二阶段 - 爬虫入门 🎯 今日目标 掌握网页分页的原理和定位“下一页”的链接能编写循环逻辑自动翻页抓取内容将多页抓取整合到爬虫系统中 📘 学习内容详解 🔁 网页分页逻辑介绍 以 quotes.toscrape.com 为例: 首页链…

Python 第二阶段 - 爬虫入门

🎯 今日目标

  • 掌握网页分页的原理和定位“下一页”的链接
  • 能编写循环逻辑自动翻页抓取内容
  • 将多页抓取整合到爬虫系统中

📘 学习内容详解

  1. 🔁 网页分页逻辑介绍
    以 quotes.toscrape.com 为例:
  • 首页链接:https://quotes.toscrape.com/
  • 下一页链接:<li class="next"><a href="/page/2/">Next</a></li>

我们可以通过 BeautifulSoup 查找li.next > a['href'] 获取下一页地址,并拼接 URL。

  1. 🧪 核心思路伪代码

    while True:1. 请求当前页 URL2. 解析 HTML,提取所需内容3. 判断是否存在下一页链接- 如果有,拼接新 URL,继续循环- 如果没有,break 退出循环
    

💻 示例代码(多页抓取)

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoindef scrape_all_quotes(start_url):quotes = []url = start_urlwhile url:print(f"正在抓取:{url}")res = requests.get(url)soup = BeautifulSoup(res.text, 'lxml')for quote_block in soup.find_all("div", class_="quote"):quote_text = quote_block.find("span", class_="text").text.strip()author = quote_block.find("small", class_="author").text.strip()tags = [tag.text for tag in quote_block.find_all("a", class_="tag")]quotes.append({"quote": quote_text,"author": author,"tags": tags})# 查找下一页next_link = soup.select_one("li.next > a")if next_link:next_href = next_link['href']url = urljoin(url, next_href)  # 拼接为完整URLelse:url = Nonereturn quotesif __name__ == "__main__":all_quotes = scrape_all_quotes("https://quotes.toscrape.com/")print(f"共抓取到 {len(all_quotes)} 条名言")# 示例输出前3条for quote in all_quotes[:3]:print(f"\n{quote['quote']}\n—— {quote['author']}|标签:{', '.join(quote['tags'])}")

🧠 今日练习任务

  • 修改已有爬虫,实现抓取所有页面的名言数据

  • 使用 len() 查看共抓取多少条数据

  • 额外挑战:将所有数据保存为 JSON 文件(使用 json.dump)

    练习代码:

    import requests
    from bs4 import BeautifulSoup
    from urllib.parse import urljoin
    import jsondef scrape_all_quotes(start_url):quotes = []url = start_urlwhile url:print(f"抓取页面:{url}")response = requests.get(url)soup = BeautifulSoup(response.text, "lxml")quote_blocks = soup.find_all("div", class_="quote")for block in quote_blocks:text = block.find("span", class_="text").text.strip()author = block.find("small", class_="author").text.strip()tags = [tag.text for tag in block.find_all("a", class_="tag")]quotes.append({"quote": text,"author": author,"tags": tags})# 找到下一页链接next_link = soup.select_one("li.next > a")if next_link:next_href = next_link['href']url = urljoin(url, next_href)else:url = Nonereturn quotesif __name__ == "__main__":start_url = "https://quotes.toscrape.com/"all_quotes = scrape_all_quotes(start_url)print(f"\n共抓取到 {len(all_quotes)} 条名言。\n")# 保存到 JSON 文件output_file = "quotes.json"with open(output_file, "w", encoding="utf-8") as f:json.dump(all_quotes, f, ensure_ascii=False, indent=2)print(f"数据已保存到文件:{output_file}")
    

    运行输出:

    正在抓取:https://quotes.toscrape.com/
    正在抓取:https://quotes.toscrape.com/page/2/
    正在抓取:https://quotes.toscrape.com/page/3/
    正在抓取:https://quotes.toscrape.com/page/4/
    正在抓取:https://quotes.toscrape.com/page/5/
    正在抓取:https://quotes.toscrape.com/page/6/
    正在抓取:https://quotes.toscrape.com/page/7/
    正在抓取:https://quotes.toscrape.com/page/8/
    正在抓取:https://quotes.toscrape.com/page/9/
    正在抓取:https://quotes.toscrape.com/page/10/
    共抓取到 100 条名言
    数据已保存到文件:quotes.json
    

    quotes.json文件输出:

    [{"quote": "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”","author": "Albert Einstein","tags": ["change","deep-thoughts","thinking","world"]},{"quote": "“It is our choices, Harry, that show what we truly are, far more than our abilities.”","author": "J.K. Rowling","tags": ["abilities","choices"]},{"quote": "“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”","author": "Albert Einstein","tags": ["inspirational","life","live","miracle","miracles"]},... # 此处省去95条数据{"quote": "“A person's a person, no matter how small.”","author": "Dr. Seuss","tags": ["inspirational"]},{"quote": "“... a mind needs books as a sword needs a whetstone, if it is to keep its edge.”","author": "George R.R. Martin","tags": ["books","mind"]}
    

📎 小技巧

  • urljoin(base_url, relative_path) 可以自动拼接绝对路径

  • 网站有时采用 JavaScript 动态分页 —— 这类网站需用 Selenium/Playwright(后续学习)

📝 今日总结

  • 学会了如何从网页中提取“下一页”链接
  • 掌握了自动翻页抓取逻辑的实现方式
  • 距离构建完整的数据采集工具更进一步

文章转载自:

http://HCwPmvlF.LLtdf.cn
http://6mfleRSq.LLtdf.cn
http://XXksVm0h.LLtdf.cn
http://3AqGzMSz.LLtdf.cn
http://VCo18CwU.LLtdf.cn
http://8KKf9MJK.LLtdf.cn
http://oROECsh1.LLtdf.cn
http://h6G3VCSI.LLtdf.cn
http://eLmbdewQ.LLtdf.cn
http://o7r5ojkm.LLtdf.cn
http://3UW3ZphC.LLtdf.cn
http://XujFzhV5.LLtdf.cn
http://ekth5Mn8.LLtdf.cn
http://ylKKus2V.LLtdf.cn
http://f8iK5toL.LLtdf.cn
http://JHjrDKiw.LLtdf.cn
http://5zST9qYE.LLtdf.cn
http://yQPGkB2V.LLtdf.cn
http://zaP6OSCd.LLtdf.cn
http://xGpm9pp3.LLtdf.cn
http://rMUlcnMf.LLtdf.cn
http://BcvEfp2J.LLtdf.cn
http://l9W3CIhJ.LLtdf.cn
http://s1MvEyQC.LLtdf.cn
http://hdeRbU93.LLtdf.cn
http://t0iimdzw.LLtdf.cn
http://C9pzGVrX.LLtdf.cn
http://TrYxWixD.LLtdf.cn
http://FLSQ9BEX.LLtdf.cn
http://i5ijCrme.LLtdf.cn
http://www.dtcms.com/wzjs/696944.html

相关文章:

  • 宜昌 医院 网站建设拖拽式建站平台
  • 企业定制app湘潭关键词优化公司
  • 如何给网站添加cnzz贪玩游戏原始传奇官网
  • 中国空间站24小时直播入口网站做广告如何做帐
  • 申请免费网站域名成全视频免费观看在线看第7季动漫
  • 仿做网站网站crm管理软件
  • 济南市历城区精神文明建设网长沙seo排名扣费
  • 泰州网站制作价格经典设计产品
  • 专门做地图的网站网站的备案要求吗
  • 手机网站开发开发wordpress 公告插件
  • 美乐乐网站源码微信小程序网页版
  • 价格划算的东莞建网站公司泰州模板开发建站
  • 江岸区网站公司房产网站开发报价
  • 现代化专业群建设专题网站wordpress建站发文教程
  • 做设计的什么网站能挣钱专业定制小程序
  • 营销网站设计方案wordpress悬浮微信电话
  • 做断桥铝窗户的网站重庆网站建设及推广公司
  • 苍梧县网站建设搜索引擎推广的简称是
  • 商城顺德网站建设北京市建设工程
  • 医院网站建设多少钱为什么建设银行网站
  • 武昌做网站中国建设网网站
  • 做网站必须要推广吗国外网站赏析
  • 中国空间站实时位置wordpress询盘功能
  • 汕头seo网站建设湖南企业建网站公司
  • 创建公司网站需要准备哪些素材小程序商城需要icp许可证吗
  • 广西网站建设-好发信息网wordpress a
  • 网站维护什么情况o2o网站建设要多少钱
  • 山东省建设局注册中心网站wordpress怎么引用single
  • 微信官方网站开发天水模板型网站建设
  • 万网如何建设网站阳江房价