2017-05-19 3 views
-3

Ich versuche, meinen regulären Ausdruck zu erhalten, um den Preis eines Artikels nur aus einer XML-Datei auszuspucken. Dies ist, was ich derzeit habe, aber es druckt den gesamten Text. Ich möchte nur, um den Preis ($ 95) auszudrucken. Was mache ich falsch?Python Regex - keine Druckwerte erwartet

Dies ist mein regex: <span class="price" id="product-price-[0-9]*">\$([0-9]*?\.[0-9]*)

Code:

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 

die findall vergessen ...

+6

„Was mache ich falsch?“ Parsen von XML mit Regex. Bitte verwenden Sie einen XML-Parser – e4c5

+0

Auch dort gibt es keinen findall Anruf dort – pointerless

+0

Sie haben eine Antwort; Entschuldige die Frage nicht. Entweder die Antwort akzeptieren oder die Frage löschen. – chepner

Antwort

-2

Sie wahrscheinlich nicht wollen, regexp verwenden XML-Analyse zu tun. Sie besser einen robusten XML-Parser verwenden, zum Beispiel BeautifulSoup

Siehe folgende Beispielcode

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 

die das Ergebnis gibt Sie wollen:

$ python test.py 
$95.00 
+0

abhängig von Ihrer Anwendung möchten Sie vielleicht anders, aber immer noch Xml Parser nicht Regex – Drako