2016-04-01 10 views
0

私はちょうど$から価格データを抽出したいと思います。ファイルには複数の価格があり、class = "price price-label">の後に来るものだけが必要です>Pythonフィルタの問題で再投稿

私は完全なコードを貼り付けました - 私は午前情報フォームを引っ張ってください。file.txt - 私の希望する出力は、名前と価格を並べて表示することです。私は前に美しいスープを使っていません。

data-default-alt="Ford Truck">  </h3>  </a>   </div>  <div class="tileInfo">    <div class="swatchesBox--empty"></div>              <div class="promo-msg-text">   <span class="calloutMsg-promo-msg-text"></span>   </div>        <div class="pricecontainer" data-pricetype="Stand Alone">    <p id="price_206019013" class="price price-label ">     $1,000.00    </p> 

マイコード

with open("targetbubbles.txt") as str: 
    st = str.read() 
    #print st 

import re 

#brand=re.search('data-default-title=\"(.*?)" ',st) 

#cost=re.search('\$(\d+,?\d*\.\d+)</p>',st) 
+0

私はあなたがこのためにXMLパーサのいくつかの種類を使用している願って、この種のに役立つモジュールのですか?あなたのコードを示してください。 – idjaw

答えて

1

beautifulsoupはがらくた

>>> import bs4 
>>> s = '''  <p id="price_206019013" class="price price-label ">     $2.84    </p>            <p class="regularprice-label">  Reg.  <span class="screen-reader-only"> price</span>  <span class="strike">  $2.99  </span>  </p>     <div class="eyeBrow sale-msg">  <span ''' 
>>> soup = bs4.BeautifulSoup(s, 'lxml') 
>>> soup.find_all('p', class_='price price-label ') 
[<p class="price price-label " id="price_206019013">     $2.84    </p>] 
>>> [result] = soup.find_all('p', class_='price price-label ') 
>>> result.text.strip(' $') 
u'2.84' 
+0

ありがとう!美しいスープに慣れていないので、私はいくつかの編集をしました。私は挑発的にこの後ショットを与えるだろう。 – turtle02