2015-10-01 47 views
0

我有一个字符串如何从一个特定的字符串获取子的一部分在python

str = <iframe width="420" height="315" src="https://www.youtube-nocookie.com/embed/jc8zayasOB8" frameborder="0" allowfullscreen></iframe> 

,我想借此在子SRC - 即“https://www.youtube-nocookie.com/embed/jc8zayasOB8”。那么我们如何才能做到这一点。 我试过这个

my_string="hello python world , i'm a beginner " 
print my_string.split("world",1)[1] . 

但它给了世界之后的所有字符串。我只想要src里面的子字符串。

回答

1

我的解决办法:

string = '<iframe width="420" height="315" src="https://www.youtube-nocookie.com/embed/jc8zayasOB8" frameborder="0" allowfullscreen></iframe>' 
start_src = string.find('src="') 
first_quote = string.find('"', start_src) 
end_quote = string.find('"', first_quote+1) 

print string[first_quote+1:end_quote] 
+0

非常感谢亲爱的朋友 – manuthalasseril

相关问题