最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • numpy稀疏矩陣的實(shí)現(xiàn)

    numpy稀疏矩陣的實(shí)現(xiàn)

    本文主要介紹了numpy稀疏矩陣的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • python實(shí)現(xiàn)多張圖片拼接成大圖

    python實(shí)現(xiàn)多張圖片拼接成大圖

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多張圖片拼接成大圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python升級(jí)pip及失敗處理方式

    python升級(jí)pip及失敗處理方式

    這篇文章主要介紹了python升級(jí)pip及失敗處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 用python + openpyxl處理excel2007文檔思路以及心得

    用python + openpyxl處理excel2007文檔思路以及心得

    最近要幫做RA的老姐寫(xiě)個(gè)合并excel工作表的腳本……源數(shù)據(jù)是4000+個(gè)excel 工作表,分布在9個(gè)xlsm文件里,文件內(nèi)容是中英文混雜的一些數(shù)據(jù),需要從每張表中提取需要的部分,分門(mén)別類(lèi)合并到多個(gè)大的表里。
    2014-07-07
  • 如何使用python批量修改文本文件編碼格式

    如何使用python批量修改文本文件編碼格式

    把文本文件的編碼格式進(jìn)行批量幻化,比如ascii, gb2312, utf8等,相互轉(zhuǎn)化,字符集的大小來(lái)看,utf8>gb2312>ascii,因此最好把gb2312轉(zhuǎn)為utf8,否則容易出現(xiàn)亂碼,這篇文章主要介紹了如何使用python批量修改文本文件編碼格式,需要的朋友可以參考下
    2023-03-03
  • DataFrame 將某列數(shù)據(jù)轉(zhuǎn)為數(shù)組的方法

    DataFrame 將某列數(shù)據(jù)轉(zhuǎn)為數(shù)組的方法

    下面小編就為大家分享一篇DataFrame 將某列數(shù)據(jù)轉(zhuǎn)為數(shù)組的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • PyTorch中permute的基本用法示例

    PyTorch中permute的基本用法示例

    pytorch中的permute就像是numpy中的transpose()函數(shù)一樣,根據(jù)指定的維度進(jìn)行轉(zhuǎn)置,下面這篇文章主要給大家介紹了關(guān)于PyTorch中permute的基本用法,需要的朋友可以參考下
    2022-04-04
  • Python中的Socket 與 ScoketServer 通信及遇到問(wèn)題解決方法

    Python中的Socket 與 ScoketServer 通信及遇到問(wèn)題解決方法

    Socket有一個(gè)緩沖區(qū),緩沖區(qū)是一個(gè)流,先進(jìn)先出,發(fā)送和取出的可自定義大小的,如果取出的數(shù)據(jù)未取完緩沖區(qū),則可能存在數(shù)據(jù)怠慢。本文通過(guò)實(shí)例代碼給大家介紹Python中的Socket 與 ScoketServer 通信及遇到問(wèn)題解決方法 ,需要的朋友參考下吧
    2019-04-04
  • python爬取豆瓣電影TOP250數(shù)據(jù)

    python爬取豆瓣電影TOP250數(shù)據(jù)

    這次以豆瓣電影TOP250網(wǎng)為例編寫(xiě)一個(gè)爬蟲(chóng)程序,并將爬取到的數(shù)據(jù)(排名、電影名和電影海報(bào)網(wǎng)址)存入MySQL數(shù)據(jù)庫(kù)中。
    2021-05-05
  • Python3中l(wèi)ambda表達(dá)式與函數(shù)式編程講解

    Python3中l(wèi)ambda表達(dá)式與函數(shù)式編程講解

    今天小編就為大家分享一篇關(guān)于Python3中l(wèi)ambda表達(dá)式與函數(shù)式編程講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01

最新評(píng)論

峨边| 赫章县| 西青区| 朝阳区| 乐平市| 广西| 岳西县| 株洲县| 天镇县| 乡城县| 德保县| 仙桃市| 抚远县| 土默特左旗| 旌德县| 依兰县| 陕西省| 赫章县| 诏安县| 白朗县| 中江县| 万全县| 墨竹工卡县| 应城市| 论坛| 台中县| 梧州市| 民和| 仪陇县| 临猗县| 广丰县| 通州区| 柘荣县| 奎屯市| 龙南县| 华宁县| 平阳县| 南宁市| 南川市| 琼结县| 和平区|