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.
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!
newString only has FRnrKzOrp7M.PHP Code:
string = "http://www.youtube.com/watch?v=FRnrKzOrp7M"
newString = string.replace("http://www.youtube.com/watch?v=", "")
<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 : )
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!
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
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!
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 : )
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!
More about Regular expressions:
http://docs.python.org/dev/howto/regex.html
*edited*
Result: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(regexp, re.IGNORECASE)
result = regexp.search(ytburl)
if (result):
print "String: %s" % result.group()
else:
print "Not found."
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 *.String: v=FRnrKzOrp7M
------------------
(program exited with code: 0)
Press return to continue
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
In Ruby, you would do something like
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.Code:/v=([^&])/.match url video_id = $1
Bookmarks