欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python讀取本地文件并解析網(wǎng)頁(yè)元素的方法

 更新時(shí)間:2018年05月21日 14:08:22   作者:林毅洋  
今天小編就為大家分享一篇Python讀取本地文件并解析網(wǎng)頁(yè)元素的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

如下所示:

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>&copy; Mugglecoding</p>
 </div>
</body>
</html>

以上這篇Python讀取本地文件并解析網(wǎng)頁(yè)元素的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Python實(shí)現(xiàn)圖像的傅里葉變換

    基于Python實(shí)現(xiàn)圖像的傅里葉變換

    傅里葉變換是一種函數(shù)在空間域和頻率域的變換,從空間域到頻率域的變換是傅里葉變換,而從頻率域到空間域是傅里葉的反變換。這篇文章主要為大家介紹的是通過(guò)Python實(shí)現(xiàn)圖像的傅里葉變換,感興趣的可以了解一下
    2021-12-12
  • python自動(dòng)化神器pyautogui使用步驟

    python自動(dòng)化神器pyautogui使用步驟

    這篇文章主要給大家介紹了關(guān)于python自動(dòng)化神器pyautogui使用步驟的相關(guān)資料,在Python當(dāng)中不僅代碼簡(jiǎn)單,而且有著非常豐富的模塊,pyautogui就可以稱(chēng)之為自動(dòng)化操作的"神器",需要的朋友可以參考下
    2023-07-07
  • python代碼實(shí)現(xiàn)猜拳小游戲

    python代碼實(shí)現(xiàn)猜拳小游戲

    這篇文章主要為大家詳細(xì)介紹了python代碼實(shí)現(xiàn)猜拳小游戲,以文本文件存儲(chǔ)與調(diào)用信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • opencv python 傅里葉變換的使用

    opencv python 傅里葉變換的使用

    這篇文章主要介紹了opencv python 傅里葉變換的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python安裝Pytorch最新圖文教程

    Python安裝Pytorch最新圖文教程

    這篇文章主要介紹了Python安裝Pytorch最新圖文教程,這里總結(jié)一下Pytorch的安裝教程,做好最快、最簡(jiǎn)單、最好地完成安裝,需要的朋友可以參考下
    2023-04-04
  • Python判斷遠(yuǎn)程服務(wù)器上Excel文件是否被人打開(kāi)的方法

    Python判斷遠(yuǎn)程服務(wù)器上Excel文件是否被人打開(kāi)的方法

    這篇文章主要介紹了Python如何判斷遠(yuǎn)程服務(wù)器上Excel文件是否被人打開(kāi),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • python實(shí)現(xiàn)橫向拼接圖片

    python實(shí)現(xiàn)橫向拼接圖片

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)橫向拼接圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • pandas刪除部分?jǐn)?shù)據(jù)后重新生成索引的實(shí)現(xiàn)

    pandas刪除部分?jǐn)?shù)據(jù)后重新生成索引的實(shí)現(xiàn)

    這篇文章主要介紹了pandas刪除部分?jǐn)?shù)據(jù)后重新生成索引的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • django中的自定義分頁(yè)器的實(shí)現(xiàn)示例

    django中的自定義分頁(yè)器的實(shí)現(xiàn)示例

    本文主要介紹了django中的自定義分頁(yè)器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Python中的?if?語(yǔ)句及使用方法

    Python中的?if?語(yǔ)句及使用方法

    這篇文章主要介紹了Python中的?if?語(yǔ)句及使用方法,包括條件測(cè)試、if?-else?語(yǔ)句、if?-elif-else?語(yǔ)句以及使用?if?語(yǔ)句處理列表操作,下面內(nèi)容詳細(xì)介紹組要的小伙伴可以參考一下
    2022-03-03

最新評(píng)論