gpt4 book ai didi

python-3.x - 美汤: List all Attributes

转载 作者:行者123 更新时间:2023-12-02 09:27:31 25 4
gpt4 key购买 nike

我是一名设计研究员。我有几个 .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')

最佳答案

您要列出的内容称为属性而不是标签。要访问标签属性,请使用 .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')

关于python-3.x - 美汤: List all Attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com