如何从 python 中的特定字符串中获取子字符串的一部分
How to get a part of a substring from a particular string in 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] .
但它给出了 world 之后的所有字符串。我只想要 src.
中的子字符串
我的解决方案:
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]
我有一个字符串
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] .
但它给出了 world 之后的所有字符串。我只想要 src.
中的子字符串我的解决方案:
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]