2016-04-13 77 views
1

我是一位设计研究员。我有包含75-100报价,而我给了各种标签,像这样几个.txt文件:美丽的汤:列出所有属性

<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> 

我想尝试列出所有的标签(“69_a”“练习”,“积极”)通过使用beautifulsoup。但不是给我的输出看起来像这样:

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

是你问这是什么不清楚。请使用您的问题上的[编辑](http://stackoverflow.com/q/36597494/3100115)链接来显示您的文件内容和预期输出的样本。 – styvane

回答

0

你想要列出的是所谓的属性而不是标签。要访问标签属性,请使用.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') 
+0

非常感谢你;它的工作现在! –