加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:11:33 所属栏目:Python 来源:网络整理
导读:如下所示: from bs4 import BeautifulSouppath = './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

如下所示:

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读写txt文本文件的操作方法全解析
  • 读取本地json文件,解析json(实例讲解)
  • Python解析并读取PDF文件内容的方法
  • python逐行读取文件内容的三种方法
  • Python文件读取的3种方法及路径转义

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读