国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-01-04 15:01:36
字體:
供稿:網(wǎng)友

如下所示:

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 top 10 beachesHow to get tannedHow to be an Aussie beach bumSummer'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讀取本地文件并解析網(wǎng)頁元素的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平阳县| 龙胜| 门头沟区| 石楼县| 新乡县| 资源县| 旬阳县| 龙海市| 沭阳县| 禄丰县| 滨州市| 富宁县| 永康市| 日土县| 杭锦后旗| 屏东县| 安图县| 浦县| 海林市| 哈尔滨市| 大石桥市| 新泰市| 景洪市| 潞西市| 新营市| 衡水市| 布尔津县| 孙吴县| 浦县| 汉寿县| 东阳市| 昔阳县| 保定市| 三亚市| 繁昌县| 革吉县| 洞头县| 深泽县| 枣阳市| 仙桃市| 宣武区|