2015-10-01 56 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

相關問題