Python读取本地文件并解析网页元素的方法

yipeiwu_com5年前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>

以上这篇Python读取本地文件并解析网页元素的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python常用模块os.path之文件及路径操作方法

以下是 os.path 模块的几种常用方法: 方法 说明 os.path.abspath(path...

python实现可逆简单的加密算法

python实现可逆简单的加密算法

最近想把word密码文件的服务器密码信息归档到mysql数据库,心想着如果直接在里面写明文密码会不会不安全,如果用sha这些不可逆的算法又没法还原回来,所以自己就想着用Python写一个...

python基础while循环及if判断的实例讲解

wlile循环 while True表示永远为真,不管是什么条件都会向下执行,下面是写的一个例子。 #!/usr/bin/env python age = 24           ...

基于Python的ModbusTCP客户端实现详解

基于Python的ModbusTCP客户端实现详解

前言 Modbus协议是由Modicon公司(现在的施耐德电气Schneider Electric)推出,主要建立在物理串口、以太网TCP/IP层之上,目前已经成为工业领域通信协议的业界...

获取Pytorch中间某一层权重或者特征的例子

问题:训练好的网络模型想知道中间某一层的权重或者看看中间某一层的特征,如何处理呢? 1、获取某一层权重,并保存到excel中; 以resnet18为例说明: import t...