#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Finding the value that comes after string.find


    I know the title doesn't make much sense, but I didnt know how to explain my problem.

    Anywho, I've opened a page's source in URLLIB
    python Code:
    starturlsource = starturlopen.read() 
    string.find(starturlsource, '<a href="/profile.php?id=')

    And I used string.find to find a specific area in the page's source.
    I want to store what comes after ?id= in a variable.
    Can someone help me with this?
    I dont want to do any html parsing.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Code:
    idx = starturlsource.find('<a href="/profile.php?id=')
    if idx > -1:
         idx += len('<a href="/profile.php?id=')
         idx2 = starturlsource.find('"', idx)
         if idx2 > -1:
             data = starturlsource[idx:idx2]
             print data
         else:
             print "Could not find terminator for URL"     
    else:
         print "Could not find start tag for URL"
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    That's what I was looking for. It doesnt seem to be working though....
    Any suggestions?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Could it be that you just need to make the assignment or return the value in Scorpion's code where he has "print"?
    Code:
    S = '<a href="/profile.php?id='    ################
    my_variable = None                 ################
    
    idx = starturlsource.find(S)
    if idx > -1:
         idx += len(S)
         idx2 = starturlsource.find('"', idx)
         if idx2 > -1:
             data = starturlsource[idx:idx2]
             my_variable = data        ################
         else:
             print "Could not find terminator for URL"     
    else:
         print "Could not find start tag for URL"
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo