脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python读取本地文件并解析网页元素的方法

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

2021-02-22 00:39林毅洋 Python

今天小编就为大家分享一篇Python读取本地文件并解析网页元素的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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
?
1
2
3
4
#其中
titles = Soup.select('ul > li > div.article-info > h3 > a')
#等效
titles = Soup.select('h3 a')
?
1
2
3
4
print(title.text)
#等效
print(title.get_text())
print(title.string)

也可以使用以下代码

?
1
2
3
4
5
6
7
8
9
10
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原文:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<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读取本地文件并解析网页元素的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/kajweb/article/details/54745114

延伸 · 阅读

精彩推荐