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搭建微信公众平台
Feb 16 Python
centos 安装python3.6环境并配置虚拟环境的详细教程
Feb 22 Python
利用Python如何实现数据驱动的接口自动化测试
May 11 Python
详解Python的循环结构知识点
May 20 Python
python爬虫开发之urllib模块详细使用方法与实例全解
Mar 09 Python
Python numpy多维数组实现原理详解
Mar 10 Python
Python操作Jira库常用方法解析
Apr 10 Python
python异常处理之try finally不报错的原因
May 18 Python
详解Python高阶函数
Aug 15 Python
Python编写单元测试代码实例
Sep 10 Python
pytest fixtures装饰器的使用和如何控制用例的执行顺序
Jan 28 Python
Python实现Telnet自动连接检测密码的示例
Apr 16 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
一个很方便的 XML 类!!原创的噢
2006/10/09 PHP
十天学会php(3)
2006/10/09 PHP
使用php+xslt在windows平台上
2006/10/09 PHP
twig里使用js变量的方法
2016/02/05 PHP
PHP将页面中点击数量高的链接进行高亮显示的方法
2016/05/30 PHP
php创建类并调用的实例方法
2019/09/25 PHP
javascript 写的一个简单的timer
2009/07/30 Javascript
jquery(live)中File input的change方法只起一次作用的解决办法
2011/10/21 Javascript
jquery创建一个新的节点对象(自定义结构/内容)的好方法
2013/01/21 Javascript
自定义的一个简单时尚js下拉选择框
2013/11/20 Javascript
node.js使用require()函数加载模块
2014/11/26 Javascript
node.js中的fs.chownSync方法使用说明
2014/12/16 Javascript
JavaScript实现弹出子窗口并传值给父窗口
2014/12/18 Javascript
JS HTML5实现拖拽移动列表效果
2020/08/27 Javascript
JS实现图片延迟加载并淡入淡出效果的简单方法
2016/08/25 Javascript
基于JavaScript实现移动端无限加载分页
2017/03/27 Javascript
jQuery UI Grid 模态框中的表格实例代码
2017/04/01 jQuery
使用angular帮你实现拖拽的示例
2017/07/05 Javascript
Django使用多数据库的方法
2017/09/06 Javascript
react-redux中connect的装饰器用法@connect详解
2018/01/13 Javascript
webpack中如何使用雪碧图的示例代码
2018/11/11 Javascript
原生JavaScript实现弹幕组件的示例代码
2020/10/12 Javascript
[45:25]完美世界DOTA2联赛循环赛 PXG vs IO 第一场 11.06
2020/11/09 DOTA
Windows下安装python2和python3多版本教程
2017/03/30 Python
详谈Python 窗体(tkinter)表格数据(Treeview)
2018/10/11 Python
django 外键model的互相读取方法
2018/12/15 Python
Python代码块及缓存机制原理详解
2019/12/13 Python
Django model class Meta原理解析
2020/11/14 Python
CSS3 @font-face属性使用指南
2014/12/12 HTML / CSS
家长评语大全
2014/01/22 职场文书
《狮子和兔子》教学反思
2014/03/02 职场文书
委托书格式
2014/08/01 职场文书
2014教师党员自我评议总结
2014/09/19 职场文书
海弦WR-800F
2022/04/05 无线电
Python实现将多张图片合成MP4视频并加入背景音乐
2022/04/28 Python
了解MySQL查询语句执行过程(5大组件)
2022/08/14 MySQL