Python读取本地文件并解析网页元素的方法
作者:林毅洋 发布时间:2022-01-10 08:06:27
标签:Python,读取,本地,解析,网页
如下所示:
from bs4 import BeautifulSoup
path = './web/new_index.html'
with open(path, 'r') as f:
Soup = BeautifulSoup(f.read(), 'lxml')
titles = Soup.select('ul > li > div.article-info > h3 > a')
for title in titles:
print(title.text)
输出:
Sardinia's top 10 beaches
How to get tanned
How to be an Aussie beach bum
Summer's cheat sheet
#其中
titles = Soup.select('ul > li > div.article-info > h3 > a')
#等效
titles = Soup.select('h3 a')
print(title.text)
#等效
print(title.get_text())
print(title.string)
也可以使用以下代码
import bs4
path = './web/new_index.html'
with open(path, 'r') as f:
Soup = bs4.BeautifulSoup(f.read(), 'lxml')
titles = Soup.select('h3 a')
for title in titles:
print(title.string)
Html原文:
<html>
<head>
<link rel="stylesheet" type="text/css" href="new_blah.css" rel="external nofollow" >
</head>
<body>
<div class="header">
<img src="images/blah.png">
<ul class="nav">
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Home</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Site</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Other</a></li>
</ul>
</div>
<div class="main-content">
<h2>Article</h2>
<ul class="articles">
<li>
<img src="images/0001.jpg" width="100" height="91">
<div class="article-info">
<h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Sardinia's top 10 beaches</a></h3>
<p class="meta-info">
<span class="meta-cate">fun</span>
<span class="meta-cate">Wow</span>
</p>
<p class="description">white sands and turquoise waters</p>
</div>
<div class="rate">
<span class="rate-score">4.5</span>
</div>
</li>
<li>
<img src="images/0002.jpg" width="100" height="91">
<div class="article-info">
<h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >How to get tanned</a></h3>
<p class="meta-info">
<span class="meta-cate">butt</span><span class="meta-cate">NSFW</span>
</p>
<p class="description">hot bikini girls on beach</p>
</div>
<div class="rate">
<img src="images/Fire.png" width="18" height="18">
<span class="rate-score">5.0</span>
</div>
</li>
<li>
<img src="images/0003.jpg" width="100" height="91">
<div class="article-info">
<h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >How to be an Aussie beach bum</a></h3>
<p class="meta-info">
<span class="meta-cate">sea</span>
</p>
<p class="description">To make the most of your visit</p>
</div>
<div class="rate">
<span class="rate-score">3.5</span>
</div>
</li>
<li>
<img src="images/0004.jpg" width="100" height="91">
<div class="article-info">
<h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Summer's cheat sheet</a></h3>
<p class="meta-info">
<span class="meta-cate">bay</span>
<span class="meta-cate">boat</span>
<span class="meta-cate">beach</span>
</p>
<p class="description">choosing a beach in Cape Cod</p>
</div>
<div class="rate">
<span class="rate-score">3.0</span>
</div>
</li>
</ul>
</div>
<div class="footer">
<p>© Mugglecoding</p>
</div>
</body>
</html>
来源:https://blog.csdn.net/kajweb/article/details/54745114
0
投稿
猜你喜欢
- 1。注意用SQL分析器可以看select出来的东西select right(convert(varchar(30),getdate(),12
- 前言每条if语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试。Python根据条件测试的值为True还是Fals
- Python 是一种高级的,动态的,多泛型的编程语言。Python代码很多时候看起来就像是伪代码一样,因此你可以使用很少的几行可读性很高的代
- Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。一、创建字典字典由键和对应值成对组成。字典也
- 常用的标准库序列化模块import pickle序列化和反序列化把不能直接存储的数据变得可存储,这个过程叫做序列化。把文件中的数据拿出来,回
- 从MySQL 5.0.2开始,通过mysql_stmt_attr_set() C API函数实现了服务器端光标。服务器端光标允许在服务器端生
- 一、前言python在数组中随机取值有现成的方法,但是要给每个随机值被取到的概率加权重的话,可以参考下面这个方法二、实现方式import r
- 效果展示打地鼠小游戏简介打地鼠的游戏规则相信大家都知道,这里就不多介绍了,反正就是不停地拿锤子打洞里钻出来的地鼠呗~首先,让我们确定一下游戏
- 一直以来,我们大多使用js来实现弹出菜单,可是根据 w3c 的css标准,根本就没有这个必要。只需要简单得使用css+html就可以做出一个
- 随着PHP4.0和JSP技术的推出以及IIS中不断出现的重大的安全问题,MicroSoft的ASP的市场仿佛是变的狭窄了,但是 MicroS
- 本文实例为大家分享了React实现表格选取的具体代码,供大家参考,具体内容如下在工作中,遇到一个需求,在表格中实现类似于Excel选中一片区
- 本文实例讲述了php基于协程实现异步的方法。分享给大家供大家参考,具体如下:github上php的协程大部分是根据这篇文章实现的:http:
- 数制转换即进制转换,指进制(二、八、十、十六进制)间的相互转换,计算机编程中较为常见。这里列举了python常见数制转换用法。1.进位制度P
- 使用python制作好看的时钟,供大家参考,具体内容如下游戏用到初高中使用的三角函数等知识开发,长话短说,上完整程序。#-*- coding
- 一、特效预览处理前处理后细节放大后二、程序原理1.输入你想隐藏的文字2.然后写到另一张跟照片同等大小的空白纸张上3.将相同位置的文字的颜色用
- 简介这两天更新完Xcode8之后发现Xcode对图标的要求又有了变化,之前用的一个小应用“IconKit”还没赶上节奏,已经不能满足Xcod
- 需求背景:进行分值计算。如下图,如果只是一两个还好说,写写判断,但是如果有几十个,几百个,会不会惨不忍睹。而且,下面的还是三种情况。例如:解
- 求最大公约数是习题中比较常见的类型,下面小编会给大家提供五种比较常见的算法,记得帮忙点个赞哦!一般来说,最大公约数的求法大概有5种方法一:短
- Numpy随机矩阵: np.random.randn(d0, d1, d2, ...)矩阵大小与形状: np.ndarray.size 与
- 先写一个批处理文件,给个例子。 代码如下:set rq=%date:~0,10% exp system/system的