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
Retrieve tag which contains a text searched by regular expression
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
 
grattachecca  
View profile  
 More options Sep 14 2012, 12:14 pm
From: grattachecca <grattache...@gmail.com>
Date: Fri, 14 Sep 2012 09:14:27 -0700 (PDT)
Local: Fri, Sep 14 2012 12:14 pm
Subject: Retrieve tag which contains a text searched by regular expression

I have a bad formed HTML who contains something like

  <a style="text-decoration: none;" class="smalltitle" href="xxxxxxxxxx">
  <font style="margin: 0px; padding: 0px;" color="#000000">
  U.R.P.</font></a>

First I pass the whole HTML to the prettify() function and I obtain

  <a class="smalltitle" href="xxxxxxxxxx" style="text-decoration: none;">
   <font color="#000000" style="margin: 0px; padding: 0px;">
    U.R.P.
   </font>
  </a>

Then I need to search for *URP* or *U.R.P.* and check that it's in an <a>
(hyperlink) tag. The problem is that the find_all() function does a ==
match, but the prettify() function add always whitespaces for indentation.

This is what I do without using regular expression and just to check if my
code is right:

  from bs4 import BeautifulSoup
  import re

  html = BeautifulSoup(BeautifulSoup(open("index.html"), "lxml").prettify())

  for parent in html.find_all("font", text="\n  U.R.P.\n  ")[0].parents:
      if parent.name == "a":
          print 'found!'
          break

The next step is to substitute that ugly text="\n  U.R.P.\n  " with a
regular expression:

  from bs4 import BeautifulSoup
  import re

  html = BeautifulSoup(BeautifulSoup(open("index.html"), "lxml").prettify())

  print html.find_all(text=re.compile("\s*U\.R\.P\.\s*"))  # first try  

  print html.find_all("a", text=re.compile("\s*U\.R\.P\.\s*"))  # second try

The first find_all(), find correctly the string, BUT I need the tag, not
the string.
The second find_all() should works exactly the way I need BUT it doesn't
find anything.

What am I doing wrong?

If I was not too clear, I need to search for *URP* or *U.R.P.* in the whole
page (including eventual whitespaces and tabs) and check that it's in an
<a> (hyperlink) tag. There is a better way to do, instead of the one I'm
trying?

Cheers


 
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.
grattachecca  
View profile  
 More options Sep 17 2012, 9:27 am
From: grattachecca <grattache...@gmail.com>
Date: Mon, 17 Sep 2012 06:27:28 -0700 (PDT)
Local: Mon, Sep 17 2012 9:27 am
Subject: Re: Retrieve tag which contains a text searched by regular expression

I saw that this is a common problem. A partial solution can be

  print
html.find_all(text=re.compile("\s*U\.R\.P\.\s*"))[0].find_parent("a")

Using find_parent() function. This does not find an <a> tag with the
expression in it but find the regex and then try to check if it has a
parent tag named "a" (at any superior level).

The good part is that find_parent() return an bs4.element.Tag instead of a
bs4.element.NavigableString (returned by find_all + regular expression)


 
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.
Xavier Combelle  
View profile  
 More options Sep 14 2012, 1:34 pm
From: Xavier Combelle <xavier.combe...@free.fr>
Date: Fri, 14 Sep 2012 19:34:54 +0200
Local: Fri, Sep 14 2012 1:34 pm
Subject: Re: Retrieve tag which contains a text searched by regular expression

you should not use prettify

   from bs4 import BeautifulSoup
   import re

   html = BeautifulSoup(open("index.html"), "lxml")

   for parent in html.find_all("font", text="U.R.P.")[0].parents:
       if parent.name == "a":
           print 'found!'
           break

no need to do any regular expression


 
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 »