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

作者:Xiao|Deng 时间:2023-09-17 02:57:48 

前言

本文主要介绍的是关于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>

来源:http://www.cnblogs.com/dengyg200891/p/6060129.html

标签:python3,beautifulsoup,div
0
投稿

猜你喜欢

  • 矩形相交以及求出相交的区域的原理解析

    2023-08-17 18:39:09
  • 浅析Python编写函数装饰器

    2023-06-07 12:20:05
  • 网页语言编码及asp乱码问题解决方案

    2008-01-31 13:21:00
  • 详解Python的字符串格式化

    2022-05-01 10:35:52
  • mysql 8.0.22.0 下载安装配置方法图文教程

    2024-01-24 14:14:36
  • 利用JavaScript实现拖拽改变元素大小

    2024-06-10 12:00:05
  • 利用Javascript实现一套自定义事件机制

    2024-05-03 15:59:07
  • python如何寻找主串中所有指定子串下标

    2023-11-27 08:38:41
  • Javascript HTML5 Canvas实现的一个画板

    2024-04-10 10:39:36
  • 对python numpy数组中冒号的使用方法详解

    2022-05-15 11:26:41
  • 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解

    2022-12-17 17:20:07
  • python3.6 实现AES加密的示例(pyCryptodome)

    2022-07-05 08:17:08
  • Laravel框架视图和模型操作方法分析

    2024-05-11 09:19:35
  • Pycharm学生免费专业版安装教程的方法步骤

    2022-01-11 14:39:30
  • 在 Python 中利用Pool 进行多处理

    2022-03-07 13:10:36
  • win7系统安装SQLServer2000的详细步骤(图文)

    2024-01-27 02:39:26
  • python实现图片插入文字

    2021-11-02 20:32:09
  • ASP图片分页代码 (通用)

    2009-06-22 12:57:00
  • ASP怎么读取指定xml 的节点

    2008-04-28 13:12:00
  • Python格式化输出字符串方法小结【%与format】

    2023-01-06 00:01:50
  • asp之家 网络编程 m.aspxhome.com