Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: How to parse a part of a link in python?

  1. #1
    Join Date
    Aug 2006
    Location
    Somewhere but Not Here
    Beans
    965
    Distro
    Ubuntu 8.04 Hardy Heron

    How to parse a part of a link in python?

    Hi everyone. How can I crop a part of a link in python? For example, let's say the link is http://www.youtube.com/watch?v=FRnrKzOrp7M and I want to get FRnrKzOrp7M from that link. How do I go about doing that? Thanks.
    My blog | Ubuntu Hardware List | Wine Application Database | Jamendo
    (Don't "sudo rm -rf" anything. See this page)
    It's all about fixing BUG #1!

  2. #2
    Join Date
    Feb 2007
    Beans
    314
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: How to parse a part of a link in python?

    PHP Code:
    string "http://www.youtube.com/watch?v=FRnrKzOrp7M"
    newString string.replace("http://www.youtube.com/watch?v="""
    newString only has FRnrKzOrp7M.
    <plexr> do you know std c++ ?
    <plexr> or is a weak understanding of VB your only strength
    <ahorse_> oohhhhh he just said the equivalent of *yo momma*
    www.acgla.net <--- My webpage : )

  3. #3
    Join Date
    Aug 2006
    Location
    Somewhere but Not Here
    Beans
    965
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to parse a part of a link in python?

    Thanks for the quick reply
    My blog | Ubuntu Hardware List | Wine Application Database | Jamendo
    (Don't "sudo rm -rf" anything. See this page)
    It's all about fixing BUG #1!

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to parse a part of a link in python?

    I would've suggested using regular expressions for that. Like looking for the "?v=_______" with it, since youtube can have different flags like "?locale=" for another language, etc.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  5. #5
    Join Date
    Aug 2006
    Location
    Somewhere but Not Here
    Beans
    965
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to parse a part of a link in python?

    How can I do that? What if -like you said- there was another flag?
    My blog | Ubuntu Hardware List | Wine Application Database | Jamendo
    (Don't "sudo rm -rf" anything. See this page)
    It's all about fixing BUG #1!

  6. #6
    Join Date
    Oct 2007
    Location
    Vienna, Europe
    Beans
    77

    Re: How to parse a part of a link in python?


  7. #7
    Join Date
    Feb 2007
    Beans
    314
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: How to parse a part of a link in python?

    Heres a link on how to use regulars expression on python (kinda hard though):

    http://www.amk.ca/python/howto/regex/

    Or you could replace the "http://www.youtube.com/watch?v=" with "http://www.youtube.com/watch?*="
    <plexr> do you know std c++ ?
    <plexr> or is a weak understanding of VB your only strength
    <ahorse_> oohhhhh he just said the equivalent of *yo momma*
    www.acgla.net <--- My webpage : )

  8. #8
    Join Date
    Aug 2006
    Location
    Somewhere but Not Here
    Beans
    965
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to parse a part of a link in python?

    Quote Originally Posted by Can+~ View Post
    I would've suggested using regular expressions for that. Like looking for the "?v=_______" with it, since youtube can have different flags like "?locale=" for another language, etc.
    How can I use this with replace() ? Can you give a simple example?
    My blog | Ubuntu Hardware List | Wine Application Database | Jamendo
    (Don't "sudo rm -rf" anything. See this page)
    It's all about fixing BUG #1!

  9. #9
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How to parse a part of a link in python?

    More about Regular expressions:
    http://docs.python.org/dev/howto/regex.html

    *edited*
    PHP Code:
    import re
    ytburl 
    "http://www.youtube.com/watch?v=FRnrKzOrp7M"
    regexp "v=\\w*" 

    #It's a good idea to compile it if you're gonna use it more than once.
    regexp re.compile(regexpre.IGNORECASE)
    result regexp.search(ytburl)

    if (
    result):
        print 
    "String: %s" result.group()
    else:
        print 
    "Not found." 
    Result:
    String: v=FRnrKzOrp7M


    ------------------
    (program exited with code: 0)
    Press return to continue
    I'm sure there's a lot of improvement you could do to the regular expression like using {7,} to specify a minimum of repetitions instead of using the *.
    Last edited by Can+~; March 9th, 2008 at 10:23 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  10. #10
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: How to parse a part of a link in python?

    In Ruby, you would do something like
    Code:
    /v=([^&])/.match url
    video_id = $1
    The regular expression (between, but not including, the slashes) should be the same in Python. $1 is the contents of the first perenthesized part of the regex. I don't know how to access that data in Python, but you should be able to find it easily enough. The regex is the hardest part of this.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •