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开发编码规范
Sep 08 Python
Python的Django框架可适配的各种数据库介绍
Jul 15 Python
编写自定义的Django模板加载器的简单示例
Jul 21 Python
详解python脚本自动生成需要文件实例代码
Feb 04 Python
Python简单实现socket信息发送与监听功能示例
Jan 03 Python
11个Python Pandas小技巧让你的工作更高效(附代码实例)
Apr 30 Python
pybind11和numpy进行交互的方法
Jul 04 Python
解决Python中pandas读取*.csv文件出现编码问题
Jul 12 Python
Flask框架请求钩子与request请求对象用法实例分析
Nov 07 Python
python实现逆滤波与维纳滤波示例
Feb 26 Python
python实现读取类别频数数据画水平条形图案例
Apr 24 Python
Python中用xlwt制作表格实例讲解
Nov 05 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数组去重实例及分析
2013/11/26 PHP
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
2015/03/18 PHP
跟我学Node.js(四)---Node.js的模块载入方式与机制
2014/06/04 Javascript
js style动态设置table高度
2014/10/21 Javascript
js打造数组转json函数
2015/01/14 Javascript
js实现点击左右按钮轮播图片效果实例
2015/01/29 Javascript
js弹出框、对话框、提示框、弹窗实现方法总结(推荐)
2016/05/31 Javascript
javascript时间戳和日期字符串相互转换代码(超简单)
2016/06/22 Javascript
Bootstrap实现导航栏的2种方式
2016/11/28 Javascript
vue.js指令v-model实现方法
2016/12/05 Javascript
JavaScript获取中英文混合字符串长度的方法示例
2017/02/04 Javascript
Angular父组件调用子组件的方法
2018/04/02 Javascript
解决vue路由后界面没有变化,但是链接有的问题
2018/09/01 Javascript
VUE 配置vue-devtools调试工具及安装方法
2018/09/30 Javascript
video.js 一个页面同时播放多个视频的实例代码
2018/11/27 Javascript
Vue中JS动画与Velocity.js的结合使用
2019/02/13 Javascript
一文快速详解前端框架 Vue 最强大的功能
2019/05/21 Javascript
JS实现简易贪吃蛇游戏
2020/08/24 Javascript
Vant Weapp组件踩坑:picker的初始赋值解决
2020/11/12 Javascript
[44:26]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#4EG VS Fnatic第二局
2016/03/03 DOTA
[03:35]2018年度DOTA2最佳辅助位选手5号位-完美盛典
2018/12/17 DOTA
python logging类库使用例子
2014/11/22 Python
使用Python脚本将文字转换为图片的实例分享
2015/08/29 Python
Python数据结构之顺序表的实现代码示例
2017/11/15 Python
Python中那些 Pythonic的写法详解
2019/07/02 Python
详解Python文件修改的两种方式
2019/08/22 Python
python中sort和sorted排序的实例方法
2019/08/26 Python
TensorFlow实现指数衰减学习率的方法
2020/02/05 Python
利用Python实现自动扫雷小脚本
2020/12/17 Python
Jo Malone美国官网:祖玛珑香水
2017/03/27 全球购物
法国最大电子商务平台:Cdiscount
2018/03/13 全球购物
师范应届毕业生自荐信
2013/11/18 职场文书
信访工作经验交流材料
2014/05/23 职场文书
个人作风建设总结
2014/10/23 职场文书
Mysql官方性能测试工具mysqlslap的使用简介
2021/05/21 MySQL
php实现自动生成验证码的实例讲解
2021/11/17 PHP