2016-08-22 4 views
1

私はFoursquareのデータをダウンロードしました。これはKML形式です。私はそれをPythonでXMLファイルとして解析しており、閉じたタグと閉じた記述タグの間でテキストを取得する方法を理解できません。 (これは、私がチェックインしたときに入力したテキストです。下の例では、「ここではここで!!ソニーと一緒に」と表示されていますが、ハイフンもあります)。2つの閉じたタグ間でテキストを取得するXML - Python

これは、データの外観の例です。

<Placemark> 
    <name>hummus grill</name> 
    <description>@<a href="https://foursquare.com/v/hummus-grill/4aab4f71f964a520625920e3">hummus grill</a>- FINALLY HERE!! With Sonya and co</description> 
    <updated>Tue, 24 Jan 12 17:14:00 +0000</updated> 
    <published>Tue, 24 Jan 12 17:14:00 +0000</published> 
    <visibility>1</visibility> 
    <Point> 
    <extrude>1</extrude> 
    <altitudeMode>relativeToGround</altitudeMode> 
    <coordinates>-75.20104383595685,39.9528387056977</coordinates> 
    </Point> 
</Placemark> 

これまでのところ、私は緯度/ long型を得ることができました、日付を発表し、名前、およびコードとのリンクすべてのために、このような何か:

latitudes = [] 
longitudes = [] 

for d in dom.getElementsByTagName('coordinates'): 
    #Break them up into latitude and longitude 
    coords = d.firstChild.data.split(',') 
    longitudes.append(float(coords[0])) 
    latitudes.append(float(coords[1])) 

私はこれを試してみました(下記でありますデータの始まりは、このヘッダ事があります)まだそれをどのように扱うか

for d in dom.getElementsByTagName('description'): 
    description.append(d.firstChild.data.encode('utf-8')) 

<?xml version="1.0" encoding="UTF-8"?> 
<kml><Folder><name>foursquare checkin history </name><description>foursquare checkin history </description>: 

を考え出したし、このd.firstChild.nextSibling.firstChild.data.encode(「UTF-8」で、それをアクセスしていません)しかし、それは私に "hummus grill"を与えます、私は何ですか名前タグからではなくaタグ間のテキストにすることをお勧めします。

答えて

0

次作品:

In [44]: description = [] 

In [45]: for d in dom.getElementsByTagName('description'): 
    ....:  description.append(d.firstChild.nextSibling.nextSibling.data.encode('utf-8')) 
    ....:  

In [46]: description 
Out[46]: ['- FINALLY HERE!! With Sonya and co'] 

それとも、あなたはdescriptionタグでテキスト全体をしたい場合:

from xml.dom.minidom import parse, parseString 

def getText(node, recursive = False): 
    """ 
    Get all the text associated with this node. 
    With recursive == True, all text from child nodes is retrieved 
    """ 
    L = [''] 
    for n in node.childNodes: 
     if n.nodeType in (dom.TEXT_NODE, dom.CDATA_SECTION_NODE): 
      L.append(n.data) 
     else: 
      if not recursive: 
       return None 
     L.append(getText(n)) 
    return ''.join(L) 

dom = parseString("""<Placemark> 
    <name>hummus grill</name> 
    <description>@<a href="https://foursquare.com/v/hummus-grill/4aab4f71f964a520625920e3">hummus grill</a>- FINALLY HERE!! With Sonya and co</description> 
    <updated>Tue, 24 Jan 12 17:14:00 +0000</updated> 
    <published>Tue, 24 Jan 12 17:14:00 +0000</published> 
    <visibility>1</visibility> 
    <Point> 
    <extrude>1</extrude> 
    <altitudeMode>relativeToGround</altitudeMode> 
    <coordinates>-75.20104383595685,39.9528387056977</coordinates> 
    </Point> 
</Placemark>""") 

description = [] 

for d in dom.getElementsByTagName('description'): 
    description.append(getText(d, recursive = True)) 

print description 

はこれを印刷します:[u'@hummus grill- FINALLY HERE!! With Sonya and co']

0

サブストリングを試しましたか?

あなたのxmlはすべて変数 "foo"に入っています。

foo = '<description>@<a href="https://foursquare.com/v/hummus-grill/4aab4f71f964a520625920e3">hummus grill</a>- FINALLY HERE!! With Sonya and co</description>' 

このデータは、次のように印刷して抽出できます。

foo[foo.index('</a>')+4:foo.index('</description>')] 

これは必要なものを提供します。

- FINALLY HERE!! With Sonya and co 

部分文字列を読み込むだけで簡単にテキストを操作できます。私にとって

+0

ので、私がする必要があるでしょうDOM要素を部分文字列に変換しますか?またはあなたは全く別のルートを提案していますか? – user3768258

+0

DOM要素全体を1つの変数にすると、戻って特定の部分を取り除くことが容易になります。サブストリングは、テキストを簡単に解析できる傾向があります。 –

関連する問題