如何只打印文本beautifulsoup [英] how to print only text beautifulsoup

查看:14
本文介绍了如何只打印文本beautifulsoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 beautifulsoup 的工作原理以创建应用程序.

I am trying to learn how beautifulsoup works in order to create an application.

我可以使用 .find_all() 查找和打印所有元素,但它们也会打印 html 标签.如何仅打印这些标签内的文本.

I am able to find and print all elements with .find_all() however they print the html tags as well. How can I print ONLY the text within these tags.

这就是我所拥有的:

from bs4 import BeautifulSoup

"""<html>
<p>1</p>
<p>2</p>
<p>3</p>
"""

soup = BeautifulSoup(open('index.html'), "html.parser")
i = soup.find_all('p')
print i

推荐答案

这可能对您有帮助:-

from bs4 import BeautifulSoup
source_code = """<html>
<p>1</p>
<p>2</p>
<p>3</p>
"""
soup = BeautifulSoup(source_code)
print soup.text

输出:-

1
2
3

这篇关于如何只打印文本beautifulsoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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