python 3利用BeautifulSoup抓取div标签的方法示例

发布时间 - 2026-01-11 01:28:57    点击率:

前言

本文主要介绍的是关于python 3用BeautifulSoup抓取div标签的方法示例,分享出来供大家参考学习,下面来看看详细的介绍:

示例代码:

# -*- coding:utf-8 -*-
#python 2.7
#XiaoDeng
#http://tieba.baidu.com/p/2460150866
#标签操作


from bs4 import BeautifulSoup
import urllib.request
import re


#如果是网址,可以用这个办法来读取网页
#html_doc = "http://tieba.baidu.com/p/2460150866"
#req = urllib.request.Request(html_doc) 
#webpage = urllib.request.urlopen(req) 
#html = webpage.read()



html="""
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" rel="external nofollow" class="sister" id="xiaodeng"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>;
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" class="sister" id="xiaodeng">Lacie</a>
and they lived at the bottom of a well.</p>
<div class="ntopbar_loading"><img src="http://simg.sinajs.cn/blog7style/images/common/loading.gif">加载中…</div>

<div class="SG_connHead">
   <span class="title" comp_title="个人资料">个人资料</span>
   <span class="edit">
      </span>
<div class="info_list">  
         <ul class="info_list1">
     <li><span class="SG_txtc">博客等级:</span><span id="comp_901_grade"><img src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" real_src="http://simg.sinajs.cn/blog7style/images/common/number/9.gif" /></span></li>
     <li><span class="SG_txtc">博客积分:</span><span id="comp_901_score"><strong>0</strong></span></li>
     </ul>
     <ul class="info_list2">
     <li><span class="SG_txtc">博客访问:</span><span id="comp_901_pv"><strong>3,971</strong></span></li>
     <li><span class="SG_txtc">关注人气:</span><span id="comp_901_attention"><strong>0</strong></span></li>
     <li><span class="SG_txtc">获赠金笔:</span><strong id="comp_901_d_goldpen">0支</strong></li>
     <li><span class="SG_txtc">赠出金笔:</span><strong id="comp_901_r_goldpen">0支</strong></li>
     <li class="lisp" id="comp_901_badge"><span class="SG_txtc">荣誉徽章:</span></li>
     </ul>
     </div>
<div class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" rel="external nofollow" rel="external nofollow" target="_blank">更多&gt;&gt;</a></span></div>     
<p class="story">...</p>
"""
soup = BeautifulSoup(html, 'html.parser') #文档对象



# 类名为xxx而且文本内容为hahaha的div
for k in soup.find_all('div',class_='atcTit_more'):#,string='更多'
 print(k)
 #<div class="atcTit_more"><span class="SG_more"><a href="http://blog.sina.com.cn/" rel="external nofollow" rel="external nofollow" target="_blank">更多&gt;&gt;</a></span></div>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家的支持。


# beautifulsoup  # div  # beautifulsoup获取div  # python3beautifulsoup  # Python爬虫包 BeautifulSoup 递归抓取实例详解  # python基于BeautifulSoup实现抓取网页指定内容的方法  # python使用beautifulsoup从爱奇艺网抓取视频播放  # 使用python BeautifulSoup库抓取58手机维修信息  # python BeautifulSoup使用方法详解  # python 解析html之BeautifulSoup  # Python使用BeautifulSoup库解析HTML基本使用教程  # python利用beautifulSoup实现爬虫  # python使用BeautifulSoup分页网页中超链接的方法  # Python爬虫beautifulsoup4常用的解析方法总结  # python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例  # 博客  # 的是  # 可以用  # 来看看  # 这篇文章  # 谢谢大家  # 获赠  # 文档  # 加载中  # 有疑问  # title  # story  # body  # Dormouse  # head  # html  # urlopen  # webpage  # gt  # lt 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Windows10如何更改计算机工作组_Win10系统属性修改Workgroup  Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧  Laravel如何实现API资源集合?(Resource Collection教程)  Android仿QQ列表左滑删除操作  Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能  如何在Ubuntu系统下快速搭建WordPress个人网站?  Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询  Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置  Laravel Artisan命令怎么自定义_创建自己的Laravel命令行工具完全指南  如何在景安云服务器上绑定域名并配置虚拟主机?  北京企业网站设计制作公司,北京铁路集团官方网站?  Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】  如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南  如何快速生成ASP一键建站模板并优化安全性?  Laravel怎么实现微信登录_Laravel Socialite第三方登录集成  Laravel如何使用Telescope进行调试?(安装和使用教程)  Android 常见的图片加载框架详细介绍  java ZXing生成二维码及条码实例分享  如何在云服务器上快速搭建个人网站?  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  Laravel项目如何进行性能优化_Laravel应用性能分析与优化技巧大全  html5audio标签播放结束怎么触发事件_onended回调方法【教程】  如何确认建站备案号应放置的具体位置?  佛山企业网站制作公司有哪些,沟通100网上服务官网?  Laravel Seeder填充数据教程_Laravel模型工厂Factory使用  利用JavaScript实现拖拽改变元素大小  海南网站制作公司有哪些,海口网是哪家的?  lovemo网页版地址 lovemo官网手机登录  php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】  零服务器AI建站解决方案:快速部署与云端平台低成本实践  北京网站制作的公司有哪些,北京白云观官方网站?  uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址  Windows10电脑怎么查看硬盘通电时间_Win10使用工具检测磁盘健康  JavaScript实现Fly Bird小游戏  Laravel如何使用Blade组件和插槽?(Component代码示例)  Laravel如何使用Seeder填充数据_Laravel模型工厂Factory批量生成测试数据【方法】  网站优化排名时,需要考虑哪些问题呢?  百度输入法ai组件怎么删除 百度输入法ai组件移除工具  Python正则表达式进阶教程_复杂匹配与分组替换解析  敲碗10年!Mac系列传将迎来「触控与联网」双革新  如何快速建站并高效导出源代码?  Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程  如何快速完成中国万网建站详细流程?  HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】  python中快速进行多个字符替换的方法小结  Python进程池调度策略_任务分发说明【指导】  Win11怎么开启自动HDR画质_Windows11显示设置HDR选项  网站建设保证美观性,需要考虑的几点问题!  Laravel如何自定义错误页面(404, 500)?(代码示例)  Laravel如何使用Passport实现OAuth2?(完整配置步骤)