Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Unexpected result from find_parent
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andreas  
View profile  
 More options Jul 28 2012, 3:33 am
From: Andreas <achristoffer...@gmail.com>
Date: Sat, 28 Jul 2012 00:33:20 -0700 (PDT)
Local: Sat, Jul 28 2012 3:33 am
Subject: Unexpected result from find_parent

It's probably I who have misunderstood both find_all and find_parent.

*sample copy/paste code*

tekst = '<li> <a href="http://ekstrabladet.dk/minsag/article1798780.ece"
class="txt">30/7-10 EB=Hold kft hvor er hun Dum=(Thorning) ?<em>28.
jul</em></a> </li>'
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(tekst)
context = soup.find_all(text=re.compile("Thorning"))

print context

 This returns (I had only expected it to return the string I was looking
for: "Thorning"):
*Output*
[u'30/7-10 EB=Hold kft hvor er hun Dum=(Thorning) ?']

When I look into context:

*sample copy/paste code*

for c in context:
    for i in c.find_parent():
        print "RESULT: %s" % i                                            
          ...

*
*
*Output*
RESULT: 30/7-10 EB=Hold kft hvor er hun Dum=(Thorning) ?
RESULT: <em>28. jul</em>

The first result is exactly what I would expect - but I don't understand
the second result?

Any help appreciated,
Sincerely

Andreas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leonard Richardson  
View profile  
 More options Jul 28 2012, 7:34 am
From: Leonard Richardson <leona...@segfault.org>
Date: Sat, 28 Jul 2012 07:34:12 -0400
Local: Sat, Jul 28 2012 7:34 am
Subject: Re: Unexpected result from find_parent

> context = soup.find_all(text=re.compile("Thorning"))
>  This returns (I had only expected it to return the string I was looking
> for: "Thorning"):
> Output
> [u'30/7-10 EB=Hold kft hvor er hun Dum=(Thorning) ?']

When you search by regular expression, you find the entire string that
matches the regular expression, not just the part of the string that
matches.

The prettify() method is useful when trying to understand these things:

print c.find_parent().prettify()
# <a class="txt" href="http://ekstrabladet.dk/minsag/article1798780.ece">
#  30/7-10 EB=Hold kft hvor er hun Dum=(Thorning) ?
#  <em>
#   28. jul
#  </em>
# </a>

The result of c.find_parent() is an <A> tag that contains a string and
an <EM> tag. When you iterate over a tag, you iterate over its
children. Here, the first result is the string and the second result
is the <EM> tag.

Leonard


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Christoffersen  
View profile  
 More options Jul 29 2012, 10:32 am
From: Andreas Christoffersen <achristoffer...@gmail.com>
Date: Sun, 29 Jul 2012 16:32:24 +0200
Local: Sun, Jul 29 2012 10:32 am
Subject: Re: Unexpected result from find_parent

Thanks for getting me up to speed Leonard. Everything now works as
expected! - Love BS4 - What ever other reasons there is, I find it much
easier than lxml (for my needs anyway). Also really good documentation.
Thanks again.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »