没有合适的资源?快使用搜索试试~ 我知道了~
首页Python读取本地文件并解析网页元素的方法
如下所示: 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 Auss
资源详情
资源评论
资源推荐

Python读取本地文件并解析网页元素的方法读取本地文件并解析网页元素的方法
如下所示:如下所示:
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>














安全验证
文档复制为VIP权益,开通VIP直接复制

评论0