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">
</head>
<body>
    <div class="header">
        <img src="images/blah.png">
        <ul class="nav">
            <li><a href="#">Home</a></li>
            <li><a href="#">Site</a></li>
            <li><a href="#">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">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">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">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">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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值