SoFunction
Updated on 2024-11-10

Python's method for reading local files and parsing web page elements

As shown below:

from bs4 import BeautifulSoup
path = './web/new_index.html'
with open(path, 'r') as f:
 Soup = BeautifulSoup((), 'lxml')
 titles = ('ul > li > -info > h3 > a')
for title in titles:
 print()

exports:
Sardinia's top 10 beaches
How to get tanned
How to be an Aussie beach bum
Summer's cheat sheet
# where
titles = ('ul > li > -info > h3 > a')
#equivalent
titles = ('h3 a')
print()
#equivalent
print(title.get_text())
print()

The following code can also be used

import bs4 
 
path = './web/new_index.html' 
 
with open(path, 'r') as f: 
 Soup = ((), 'lxml') 
 
 titles = ('h3 a') 
for title in titles: 
 print() 

Html Original:

<html>
<head>
 <link rel="stylesheet" type="text/css" href="new_blah.css" rel="external nofollow" >
</head>
<body>
 <div class="header">
  <img src="images/">
  <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/" width="100" height="91">
    <div class="article-info">
     <h3><a href="" 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/" width="100" height="91">
    <div class="article-info">
     <h3><a href="" 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/" width="18" height="18">
     <span class="rate-score">5.0</span>
    </div>
   </li>
   <li>
    <img src="images/" width="100" height="91">
    <div class="article-info">
     <h3><a href="" 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/" width="100" height="91">
    <div class="article-info">
     <h3><a href="" 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>

Above this Python read local files and parse web page elements is all I have shared with you, I hope to give you a reference, and I hope you support me more.