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


猜你喜欢
- 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中。将csv文件中的数据直接读取为 dict 类型和 DataFr
- 关键的安装考虑虽然大多数的SQL Server安装都使用了默认的参数,这样的话是一个简单的过程,但是没有理解安装参数的话,也会导致困惑或者将
- 实例如下所示://二维数组去掉重复值 public function a_array_unique($array){ $out
- SvnPeer.php <?php /** * * This class for execute the external progr
- 有的时候,可能会遇到表格中出现重复的索引,在操作重复索引的时候可能要注意一些问题。一、判断索引是否重复a、Series索引重复判断s = S
- 我就废话不多说了,大家还是直接看代码吧!import matplotlib.pyplot as pltimport numpy as npf
- 1.简介keplergl是由Uber开源的一款地理数据可视化工具,通过keplergl我们可以在Jupyter notebook中使用,可视
- python的annotate函数annotate函数该函数的详细参数可调用内置属性__doc__查看。import matplotlib.
- 使用echart卡在引入包的问题上了。到github下载的js一直引入不了。注意是引入dirt文件夹下的echarts.js把这个文件夹放入
- 前言微服务中的日志采集方案ELK(EFK)已经是基本事实标准了,但是单体服务中却没有像ELK这样的成熟采集方案,这与单体性质有关,单体毕竟涉
- 需求表格实现行拖拽,要求只支持同级拖拽!实现使用插件:SortableJS,可以参考官网配置项!// 安装npm install sorta
- 串口通信是指外设和计算机间,通过数据信号线 、地线、控制线等,按位进行传输数据的一种通讯方式。这种通信方式使用的数据线少,在远距离通信中可以
- 0.引言利用python开发,借助Dlib库捕获摄像头中的人脸,提取人脸特征,通过计算欧氏距离来和预存的人脸特征进行对比,达到人脸识别的目的
- 前面我通过一篇文章讲述了如何爬取CSDN的博客摘要等信息。通常,在使用Selenium爬虫爬取数据后,需要存储在TXT文本中,但是这是很难进
- 一:取字符串中第几个字符print "Hello"[0] 表示输出字符串中第一个字符print "Hello&
- 本文实例讲述了PHP+Mysql基于事务处理实现转账功能的方法。分享给大家供大家参考。具体如下:<?php header(
- 1. 前言最近有点时间,就随便找点东西弄弄,倒也碰到了一些问题,在此记录下2. 环境Python3.11.3 + selenium4.9.1
- 内容摘要:浏览器不兼容这个难题,一直是网页设计师们头痛的事情。ie7.0的面世,尚且不论他是否较之ie6.0进步, ie7和ie6
- 1.原始数据是这样的2.脚本如下:import pandas as pddf = pd.read_excel(r'E:\untitl
- 导出数据库数据:首先打开cmd进入MySQL的bin文件夹下1.导出education数据库里面的users表的表数据和表结构(下面以use