2016-04-13 9 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を使って。しかし、代わりに私にこのような出力を与える:

q 
q 
q 
q 
Finished... 

あなたは私がこの問題を解決する助けてください:

69_a 
exercises 
positive 

は、それは私にこのような出力を与えていますか?私はこれを通したい質の高いデータをたくさん持っています。目的はすべての引用を.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

が、それはあなたがここに求めているものは不明です。ご質問の[edit](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

ありがとうございました。今働いている! –

関連する問題