problem with my regex?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

    problem with my regex?

    I have a simple script below that is causing me some problems and I am
    having a hard time tracking them down. Here is the code:

    import urllib
    import re

    def getPicLinks():
    found = []
    try:
    page =
    urllib.urlopen( "http://continuouswave. com/whaler/cetacea/")
    except:
    print "ERROR RREADING PAGE."
    sys.exit()
    page1 = page.read()
    cetLinks = re.compile("cet aceaPage..\.htm l", page1)
    for line in page1:
    found.append(ce tLinks.findall( line))
    print found

    This is the error message:
    "/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/sre_parse.py",
    line 396, in _parse
    if state.flags & SRE_FLAG_VERBOS E:
    TypeError: unsupported operand type(s) for &: 'str' and 'int'

    I am trying to extract the links on a web page that have a similar
    pattern. Here is an example of the html source:

    <HR>
    <P><SMALL><A HREF="photoLog. html">PHOTO-LOG</A><br>
    <A HREF="guide.htm l">How-To-Submit</A><BR><A
    HREF="cetaceaPa ge01.html">01</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge02.html">02</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge03.html">03</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge04.html">04</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge05.html">05</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge06.html">06</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge07.html">07</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge08.html">08</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge09.html">09</A>&nbsp;|&nbsp; <A
    HREF="cetaceaPa ge10.html">10</A>
    <BR><A>

    My problem is that I can't seem to be able to figure out what is going
    wrong here. Mostly because I am a bit confused by the error message as
    it points to a file (presumable part of re) that I am unfamiliar with,
    and I am a bit new with python.

    Any help is greatly appreciated, as is your patience.

    Brian

  • Bruno Desthuilliers

    #2
    Re: problem with my regex?

    Brian a écrit :[color=blue]
    > I have a simple script below that is causing me some problems and I am
    > having a hard time tracking them down. Here is the code:
    >
    > import urllib
    > import re
    >
    > def getPicLinks():
    > found = []
    > try:
    > page =
    > urllib.urlopen( "http://continuouswave. com/whaler/cetacea/")
    > except:[/color]
    Do everyone a favor: don't use bare expect clause
    [color=blue]
    > print "ERROR RREADING PAGE."
    > sys.exit()[/color]

    stdout is for normal program outputs. Error messages should go to
    stderr. And FWIW, your exception handling here is more than useless.
    You'd better let the exception propagate - at worse, it will also exit
    the program, but with the right return value for the system and a
    meaningful traceback.
    [color=blue]
    > page1 = page.read()
    > cetLinks = re.compile("cet aceaPage..\.htm l", page1)[/color]

    Are you sure you've carefully read the doc for re.compile() ?-)

    You want something like this (NB : regexp not tested):

    html = page.read()
    page.close() # dont forget to free resources
    cetLinks = re.compile(r"ce taceaPage[0-9]{2}\.html")
    found = cetLinks.findal l(html)
    print "\n".join(found )
    [color=blue]
    > This is the error message:
    > "/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/sre_parse.py",
    > line 396, in _parse
    > if state.flags & SRE_FLAG_VERBOS E:
    > TypeError: unsupported operand type(s) for &: 'str' and 'int'[/color]

    This is not the *full* traceback.

    (snip)
    [color=blue]
    > My problem is that I can't seem to be able to figure out what is going
    > wrong here.[/color]

    What's going wrong is that you are passing the html page content as the
    second argument for re.compile(), (instead of an integer value
    representing a combination of various flags, cf the doc for the re module).
    [color=blue]
    > Mostly because I am a bit confused by the error message as
    > it points to a file (presumable part of re)[/color]

    It is.

    The last parts of the traceback are the file and line where the
    exception has been raised and the exception's message. But before, you
    had all the call stack, including the line where you called re.compile()
    with the wrong arguments. Exception tracebacks are usually really useful
    once you know how to read them.

    HTH

    Comment

    • Brian

      #3
      Re: problem with my regex?

      I sincerely appreciate your reply and the time you took to explain it.

      Thank you,
      Brian

      Comment

      Working...