[Tutor] checking if regex in pattern

Norman Khine norman at khine.net
Sun Nov 11 13:05:51 CET 2012


ok, i think i have it

On Sun, Nov 11, 2012 at 10:48 AM, Norman Khine <norman at khine.net> wrote:
> hello,
>
> i have this code
>
>>>> import re
>>>> import BeautifulSoup
>>>> matcher = re.compile(r"""<p><label><strong>Origine :<\/strong><\/label>(.*)<\/p>""")
>>>> mystring = """ <p> <label class="garanties_adm"> <strong>Les garanties :</strong></label> <img src="/images/adm/icon-garantie-fairtrade-ab.png" /> </p> <label class="garanties_adm"> <strong>Les garanties :</strong></label> <img src="/images/adm/icon-garantie-fairtrade-ab.png" /> <p> <label><strong>Origine :</strong></label> Burkina Faso, Cercle des Sécheurs                         </p> <label><strong>Origine :</strong></label> Burkina Faso, Cercle des Sécheurs                          <p> </p> <p><a href="/product/670/">Ajouter à ma liste d'envies</a></p> """
>
> I can print the text out but I am unsure how to check for the pattern
> as I would like to check if pattern exists and then extract the
> Country and Producer - in this case: Burkina Faso and  Cercle des
> Sécheurs
>
> if i try:
>
>>>> for txt in soup.findAll(text=True):
> ...     print type(txt)
> ...
> <class 'BeautifulSoup.NavigableString'>
>>>> for txt in soup.findAll(text=True):
> ...     matches = matcher.match(txt)
> ...     if matches:
> ...             print txt
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 2, in <module>
> AttributeError: 'builtin_function_or_method' object has no attribute 'match'
>>>>


>>> for txt in soup.findAll(text=True):
...     if re.search('Origine',txt,re.I):
...             print txt.next
...
 Burkina Faso, Cercle des SĂŠcheurs
 Burkina Faso, Cercle des SĂŠcheurs

>
>
> what am i missing?


More information about the Tutor mailing list