如何使用python解析html文件并获取标签之间的文本? [英] How to parse a html file and get the text which is in between the tags by using Python?

查看:1126
本文介绍了如何使用python解析html文件并获取标签之间的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在Python中解析HTML

Possible Duplicate:
Parsing HTML in Python

我已经在Internet上进行了更多搜索,以获取使用Python在标签之间的文本.你们能解释一下吗?

I have searched more over on the internet for get the text which is in between the tags by using Python. Can you guys please explain?

推荐答案

以下是使用 BeautifulSoup的示例解析HTML:

Here is an example of using BeautifulSoup to parse HTML:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup("""<html><body>
                        <div id="a" class="c1">
                            We want to get this
                        </div>
                        <div id="b">
                            We don't want to get this
                        </div></body></html>""")
print soup('div', id='a').text

此输出

We want to get this

这篇关于如何使用python解析html文件并获取标签之间的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆