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


Posted in Python onMay 28, 2017

前言

本文主要介绍的是关于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">更多>></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">更多>></a></span></div>

总结

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

Python 相关文章推荐
基于python select.select模块通信的实例讲解
Sep 21 Python
python中获得当前目录和上级目录的实现方法
Oct 12 Python
详解Python nose单元测试框架的安装与使用
Dec 20 Python
Python socket套接字实现C/S模式远程命令执行功能案例
Jul 06 Python
详解Python的hasattr() getattr() setattr() 函数使用方法
Jul 09 Python
Python脚本按照当前日期创建多级目录
Mar 01 Python
Django中reverse反转并且传递参数的方法
Aug 06 Python
python脚本监控logstash进程并邮件告警实例
Apr 28 Python
python实现简单猜单词游戏
Dec 24 Python
python 下载文件的几种方法汇总
Jan 06 Python
Python对excel的基本操作方法
Feb 18 Python
Python进程间的通信之语法学习
Apr 11 Python
Python虚拟环境virtualenv的安装与使用详解
May 28 #Python
python 调用win32pai 操作cmd的方法
May 28 #Python
Python 稀疏矩阵-sparse 存储和转换
May 27 #Python
Django基础之Model操作步骤(介绍)
May 27 #Python
python之PyMongo使用总结
May 26 #Python
Python3安装Pymongo详细步骤
May 26 #Python
Python计时相关操作详解【time,datetime】
May 26 #Python
You might like
php zip文件解压类代码
2009/12/02 PHP
php 变量未定义等错误的解决方法
2011/01/12 PHP
destoon出现验证码不显示时的紧急处理方法
2014/08/22 PHP
php修改文件上传限制方法汇总
2015/04/07 PHP
Yii2中YiiBase自动加载类、引用文件方法分析(autoload)
2016/07/25 PHP
PHP 中常量的知识整理
2017/04/14 PHP
Yii2实现ActiveForm ajax提交
2017/05/26 PHP
Laravel 创建可以传递参数 Console服务的例子
2019/10/14 PHP
使用Git实现Laravel项目的自动化部署
2019/11/24 PHP
JS中==与===操作符的比较
2009/03/21 Javascript
DIV始终居中的js代码
2014/02/17 Javascript
php的文件上传入门教程(实例讲解)
2014/04/10 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
jQuery zTree加载树形菜单功能
2016/02/25 Javascript
Vue自定义指令使用方法详解
2017/08/21 Javascript
vue router 跳转后回到顶部的实例
2018/08/31 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
[01:51]2018年度CS GO最具人气外援-完美盛典
2018/12/16 DOTA
[01:07:46]完美世界DOTA2联赛循环赛 Magma vs IO BO2第二场 11.01
2020/11/02 DOTA
跟老齐学Python之集成开发环境(IDE)
2014/09/12 Python
解决python写入带有中文的字符到文件错误的问题
2019/01/31 Python
Python小程序 控制鼠标循环点击代码实例
2019/10/08 Python
Python 静态方法和类方法实例分析
2019/11/21 Python
python with (as)语句实例详解
2020/02/04 Python
python里的单引号和双引号的有什么作用
2020/06/17 Python
Python使用xlrd实现读取合并单元格
2020/07/09 Python
eBay加拿大站:eBay.ca
2019/06/20 全球购物
酒店公关部经理岗位职责
2013/11/24 职场文书
关于工资低的辞职信
2014/01/14 职场文书
创先争优活动承诺书
2014/08/30 职场文书
学雷锋活动倡议书
2014/08/30 职场文书
优秀工作者事迹材料
2014/12/26 职场文书
中学生学习保证书
2015/02/26 职场文书
JavaScript 事件捕获冒泡与捕获详情
2021/11/11 Javascript
Python+OpenCV实现图片中的圆形检测
2022/04/07 Python
python如何为list实现find方法
2022/05/30 Python