漂亮汤:列出所有属性

5

我是一名设计研究员。我有几个.txt文件,其中包含75-100条引语,我已经给这些引语打上了不同的标签,如下所示:

<q 69_A F exercises positive> Well I think it’s very good. I thought that the exercises that Rosy did was very good. I looked at it a few times. I listened and I paid attention but I didn’t really do it on the regular. I didn’t do the exercises on a regular basis. </q>

我将尝试使用beautifulsoup列出所有标签(“69_a”、“练习”、“正面”)。但是,它并没有给我像这样的输出:
69_a
exercises
positive

它给我输出的结果看起来像这样:
q
q
q
q
Finished...

你能帮我修复这个问题吗?我有很多定性数据需要处理。目标是将所有引用导出到一个 .xlsx 文件中,并使用数据透视表进行排序。

from bs4 import BeautifulSoup
file_object = open('Angela_Q_2.txt', 'r')
soup = BeautifulSoup(file_object.read(), "lxml")
tag = soup.findAll('name')

for tag in soup.findAll(True):
    print(tag.name)
print('Finished')

2
这里你的问题表述不够清晰。请使用编辑链接,展示你文件内容的样例以及期望的输出结果。 - styvane
1个回答

15

你想要列出的东西叫做属性而不是标签。要访问标签的属性,请使用`.attr`值。

如下所示:

from bs4 import BeautifulSoup

contents = '<q tag1 tag2>Quote1</q>dome other text<q tag1 tag3>quote2</q>'

soup = BeautifulSoup(contents)

for tag in soup.findAll('q'):
    print(tag.attrs)
    print(tag.contents)
print('Finished')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接