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

学校的网站管理系统asp影视网站源码

学校的网站管理系统,asp影视网站源码,百度免费网站申请注册,三门峡住房和建设局网站Python 第二阶段 - 爬虫入门 #x1f3af; 今日目标 掌握网页分页的原理和定位“下一页”的链接能编写循环逻辑自动翻页抓取内容将多页抓取整合到爬虫系统中 #x1f4d8; 学习内容详解 #x1f501; 网页分页逻辑介绍 以 quotes.toscrape.com 为例#xff1a; 首页链…Python 第二阶段 - 爬虫入门 今日目标 掌握网页分页的原理和定位“下一页”的链接能编写循环逻辑自动翻页抓取内容将多页抓取整合到爬虫系统中 学习内容详解 网页分页逻辑介绍 以 quotes.toscrape.com 为例 首页链接https://quotes.toscrape.com/下一页链接li classnexta href/page/2/Next/a/li 我们可以通过 BeautifulSoup 查找li.next a[href] 获取下一页地址并拼接 URL。 核心思路伪代码 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.jsonwith open(output_file, w, encodingutf-8) as f:json.dump(all_quotes, f, ensure_asciiFalse, indent2)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.jsonquotes.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 persons 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://www.lakalapos1.cn/news/7917/

相关文章:

  • 个人网站课程设计报告小米新品发布会流程
  • 免费APP 微信 网站平台用网站建设费用
  • 网站后台上传内容前台首页不显示怎么样用ps做网站
  • 做旅游网站教程网站设计培训成都哪家好
  • 外贸SOHO建公司网站集团主题 wordpress
  • 国外网站备案吗旅游网站网页设计
  • 网站学什么响应式网站发展
  • seo站外优化平台如何学会推广和营销
  • 成都成华区建设局官方网站网站建设 主要学是么
  • 资源网站建设域名个人备案查询
  • 企业网站规划与建设网站规划建设与管理维护教程与实训
  • 天津市建设工程协会网站常州网站制作套餐
  • 网页游戏网站4399上海企业投资人名录
  • 手机端网站设计制作案例seo推广分析
  • 网站优化方案范文wordpress 钩子开发
  • 济南比较好的网站建设公司网站公司谁家好
  • 腾讯云自助建站门户网站怎么创建
  • 网站seo检测报告云南公司网站制作
  • 建设部网站造价咨询程序开发是什么工作
  • 网站后台中文模板wordpress修改语言包目录
  • 优秀个人网站案例企业网站html百度云
  • 三合一网站建设方案免费网站如何被百度收录
  • 做网站收入来源表甘肃住房与城乡建设部网站
  • 用jsp做电影网站的界面企业网络营销策略
  • 网站建设公司转型做什wordpress放哪
  • 知名网站制作公司青岛分公司seo优化及推广如何运营
  • asp网站vps搬家快速微信网站开发
  • 学习网站建设要报班吗wordpress home插件
  • 网站icp申请wordpress svn
  • 丹东有做公司网站的吗php企业网站源码下载