python 3利用BeautifulSoup抓取div标签的方法示例
作者:Xiao|Deng 发布时间:2023-09-17 02:57:48
标签:python3,beautifulsoup,div
前言
本文主要介绍的是关于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>
来源:http://www.cnblogs.com/dengyg200891/p/6060129.html
0
投稿
猜你喜欢
- 大家在学习python中,经常会使用到K-Means和图片压缩的,我们在此给大家分享一下K-Means和图片压缩的方法和原理,喜欢的朋友收藏
- 1.若有疑问立即检测 在出错时若能对原始代码做简单检测可以省去很多头痛问题。W3C对于XHTML与CSS 都有检测工具可用,请见 http:
- Oracle TRIM函数是很常见的函数,下面对Oracle TRIM函数的语法作了详尽的阐述说明,希望可以让您对Oracle TRIM函数
- 这篇文章主要介绍了简单了解python装饰器原理及使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 本文为大家分享了python爬取m3u8连接的视频方法,供大家参考,具体内容如下要求:输入m3u8所在url,且ts视频与其在同一路径下#!
- 本文实例为大家分享了wxPython分隔窗口的具体代码,供大家参考,具体内容如下1、分割窗口分隔窗口(wx.SplitterWindow)就
- 1.定时器Timer定时器源码实现,和自定义一个线程方式一样,都是继承Thread类,重写了run()方法,只是实现的功能是延时执行一个函数
- pygame介绍Python Pygame 是一款专门为开发和设计 2D 电子游戏而生的软件包,它支 Windows、Linux、Mac O
- Application Name(应用程序名称):应用程序的名称。如果没有被指定的话,它的值为.NET SqlClient Data Pro
- 阅读:Chapter 3 * 的表格“Misquotations are the only quotations tha are never
- 本文实例讲述了php防止sql注入中过滤分页参数的方法。分享给大家供大家参考。具体分析如下:就网络安全而言,在网络上不要相信任何输入信息,对
- 前言首先是本文总体代码,改一下图像的读取路径就可以运行了,但我还是建议大家先看后面的步骤一行行敲代码,这样效果更好:""
- 1、引言小丝:鱼哥,还记得上次写的把数据库的查询结果写入到excel这个脚本不。小鱼:嗯… 可以说不记得吗小丝:我猜你
- 一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式。该模式描述在查找文字主体时待匹配的一个或多个字
- 新闻系统,相册系统可以用用哦,简单实用,有兴趣的可以自己扩充!^_^相册截图:<?xml version="1.0"
- 由于系统自带的MySQL默认字符集不是gbk,因此给数据库的推广应用以及中文程序的开发带来极大的不便,在没完没了的GBK和UTF8的转换过程
- 工作中遇到一个很棘手的问题,交互设计师和视觉设计师在做出高保真原型后提交给前端开发工程师,最后得到的web产物从细节上和布局上都和高保真原型
- 过滤器模板层对变量的操作实际还有很多,过滤器就是其中一种。学过Linux系统的一定知道管道操作符,其可以将上一步输出直接作为下一步输入进行处
- 安装pip install faker使用简单使用本库可生成姓名、地址、电话、邮箱、公司等等一系列数据。首先导入库,实例化:from fak
- 如何用JMail同时给多人发信?在ASP中,为什么我在Jmail收件人处指定多个收件人时,像这样:JMail.AddRecipient&nb