BASIC python scraping question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    #1

    BASIC python scraping question

    Ok...
    So i'm trying to scrape a Web site and just pull off data from it periodically.
    My first goal is to just get data from a site that has constantly updating numbers...lets take www.nasdaq.com for example.

    i'm using XP and python 2.4...

    The first thing i've done is identify part of the source code that keys me into where the number is, lets take "homeindexvolum e" for this example.

    This is what I've tried...

    Code:
    import urllib2
    for line in urllib2.urlopen('http://www.nasdaq.com'):
        if "homeindexvolume" in line:
        print line

    So that gives me something like...<TD HEIGHT="24" CLASS="bubblemi ddle" ALIGN="right" id="homeindexvo lume" name="homeindex volume">1,585,6 99,200</TD>

    That's good but BUT HOW CAN I:

    1. get just the desired volume number (in this case 1,585,699,200)
    2. Then how can I get it to do it again in 1 hour?

    Thanks, sorry if this is so remedial that it hurts.

    -pc
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Patrick C
    Ok...
    So i'm trying to scrape a Web site and just pull off data from it periodically.
    My first goal is to just get data from a site that has constantly updating numbers...lets take www.nasdaq.com for example.

    i'm using XP and python 2.4...

    The first thing i've done is identify part of the source code that keys me into where the number is, lets take "homeindexvolum e" for this example.

    This is what I've tried...

    Code:
    import urllib2
    for line in urllib2.urlopen('http://www.nasdaq.com'):
        if "homeindexvolume" in line:
        print line

    So that gives me something like...<TD HEIGHT="24" CLASS="bubblemi ddle" ALIGN="right" id="homeindexvo lume" name="homeindex volume">1,585,6 99,200</TD>

    That's good but BUT HOW CAN I:

    1. get just the desired volume number (in this case 1,585,699,200)
    2. Then how can I get it to do it again in 1 hour?

    Thanks, sorry if this is so remedial that it hurts.

    -pc
    Try the regular expression module: re. It's useful in these situations. In your case you can just have it look for a certain number of numerals or more and then return that result as a string. Try googling it or try the Global Module Index.
    Does that help?

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      here's one for you
      Code:
      import urllib2,re
      page = urllib2.urlopen('http://www.nasdaq.com')
      results = page.read()
      pat = re.compile('<TD>.*name="homeindexvolume">(.*?)</TD>',re.M|re.DOTALL)
      print pat.findall(results)

      Comment

      Working...