2017-05-19 9 views
-3

XMLファイルからのみアイテムの価格を落とすために正規表現を取得しようとしています。これは私が現在持っているものですが、テキスト全体を印刷します。私は価格を印刷したいだけです($ 95)。私は間違って何をしていますか? <span class="price" id="product-price-[0-9]*">\$([0-9]*?\.[0-9]*)Python正規表現 - 値が表示されない

コード:

from re import findall 

text = '''This XML file does not appear to have any style information associated with it. The document tree is shown below. 
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"> 
<channel> 
<title> 
<![CDATA[ New Products from Main Website Store ]]> 
</title> 
<link> 
http://www.joomlajingle.com/rss/catalog/new/store_id/1/ 
</link> 
<description> 
<![CDATA[ New Products from Main Website Store ]]> 
</description> 
<pubDate>Fri, 19 May 2017 12:07:04 +0000</pubDate> 
<generator>Zend_Feed</generator> 
<language>en_US</language> 
<docs>http://blogs.law.harvard.edu/tech/rss</docs> 
<item> 
<title> 
<![CDATA[ Womens Free People White Textured Lace Mini Dress ]]> 
</title> 
<link> 
http://www.joomlajingle.com/womens-free-people-white-textured-lace-mini-dress.html 
</link> 
<description> 
<![CDATA[ 
<table><tr><td><a href="http://www.joomlajingle.com/womens-free-people-white-textured-lace-mini-dress.html"><img src="http://www.joomlajingle.com/media/catalog/product/cache/1/thumbnail/75x75/9df78eab33525d08d6e5fb8d27136e95/3/9/39247291_010_a.jpg" border="0" align="left" height="75" width="75"></a> 
]]> 
<![CDATA[ 
</td><td style="text-decoration:none;"><strong>Womens Free People White Textured Lace Mini Dress</strong>Allover lace mini dress featuring a swing skirt, cutout detailing along the waist and contrast lace along the neckline and shoulders. Grosgrain ribbon trim and adjustable ties on the wide sleeve cuff. Hidden back zip. Lined.For Love & LemonsDerived from those sun-soaked "Lemonade Stand Days," designers, owners, and BFF's Gillian Mahin and Laura Hall are the masterminds behind For Love & Lemons. Based in Los Angeles, the collection is anything but conventional. For Love & Lemons is a brand for that girl who doesn't follow trends, she makes them.<ul class="content-bullets"><li>95% Cotton</li><li>5% Nylon</li><li>Contrast: 57.2% Nylon, 27.3% Polyester</li><li>Contrast 2: 72% Nylon, 18% Cotton, 10% Rayon</li><li>Lining: 97% Polyester, 3% Spandex</li></ul><ul class="content-bullets product-care"><li>Dry Clean</li></ul><ul class="content-bullets"><li>Import</li></ul> <div class="price-box"> <p class="old-price"> <span class="price-label">Regular Price:</span> <span class="price" id="old-price-5937">$255.00</span> </p> <p class="special-price"> <span class="price-label">Special Price:</span> <span class="price" id="product-price-5937">$95.00</span> </p> </div> </td></tr></table> 
]]> 
</description> 
<pubDate>Fri, 19 May 2017 12:07:04 +0000</pubDate> 
</item> 
''' 

find_price = ('<span class="price" id="product-price-[0-9]*">\$([0-9]*?\.[0-9]*)', text) 
print find_price 

はfindAllのを忘れてしまった...あなたは可能性の高いXMLの構文解析を行うために正規表現を使用したくない

+6

「私は間違って何をやっている」:あなたはより良い

あなたが望む結果を与える次のコード例

soup = BeautifulSoup(text) import re find_price = None for cd in soup.findAll(text=True): if isinstance(cd, CData): s = BeautifulSoup(cd) prices = s('span', {'class': 'price'}) for price in prices: if price.has_key('id') and re.match('product-price-\d+', price['id']): find_price = price.text break print find_price 

を参照してください例BeautifulSoupため、堅牢なXMLパーサーを使用できますか?正規表現を使ってxmlを解析する。 xmlパーサを使用してください – e4c5

+0

またそこにfindall呼び出しがありません – pointerless

+0

あなたは答えを得ました。その問題を解体しないでください。回答を受け入れるか、質問を削除します。 – chepner

答えて

-2

この

は私の正規表現です。

$ python test.py 
$95.00 
+0

あなたのアプリケーションによって異なるかもしれないが、それでもxmlパーサーは正規表現ではない – Drako