Python读取本地文件并解析网页元素的方法
作者:林毅洋 时间:2022-01-10 08:06:27
如下所示:
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
标签:Python,读取,本地,解析,网页
0
投稿
猜你喜欢
springMVC + easyui + $.ajaxFileUpload实现文件上传注意事项
2023-09-04 09:17:26
Mac下通过brew安装指定版本的nodejs教程
2024-05-03 15:56:11
Python图像锐化与边缘检测之Sobel与Laplacian算子详解
2023-01-25 01:36:04
利用Python脚本实现传递参数的三种方式分享
2023-07-11 03:45:44
MySQL日志的详细分析实例
2024-01-25 21:35:52
js的压缩及jquery压缩探讨(提高页面加载性能/保护劳动成果)
2024-04-16 09:23:41
python翻译软件实现代码(使用google api完成)
2023-05-19 17:25:43
Linux下升级安装python3.8并配置pip及yum的教程
2022-03-08 13:17:06
python 使用elasticsearch 实现翻页的三种方式
2021-03-09 17:39:57
Python爬虫实战之用selenium爬取某旅游网站
2021-03-25 10:28:36
python3使用scrapy生成csv文件代码示例
2021-03-21 04:36:24
python用pandas读写和追加csv文件
2021-12-19 20:47:15
python颜色随机生成器的实例代码
2022-12-03 19:40:05
python中日期和时间格式化输出的方法小结
2023-07-01 11:39:31
通过字符串导入 Python 模块的方法详解
2023-10-15 03:00:56
Python中的复杂数据类型(list、tuple)
2023-06-07 10:10:19
asp 快钱网关接口 支付宝接口 财付通接口代码
2011-03-08 10:55:00
基于python实现银行管理系统
2023-11-22 01:32:18
python selenium 弹出框处理的实现
2022-12-05 14:19:19
Python使用matplotlib绘制多个图形单独显示的方法示例
2022-02-13 09:45:44