我有一个这样的字符串,想要用645w
分辨率提取链接。
"//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=620&q=85&auto=format&sharp=10&s=ed7be5af3592322971e9e2276b5338f8 620w,
//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=700&q=85&auto=format&sharp=10&s=d49ff60c0c561cb2eee8d119acba62c5 700w,
//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=645&q=85&auto=format&sharp=10&s=19e251fb9bd21787e8364c091e4a89ce 645w,
//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=465&q=85&auto=format&sharp=10&s=fd5d66ed602638295446f28c4853744d 465w"
这个位置并不总是一样,所以我想用这样的东西:
if ' 645w' in src:
index = src.find(" 645w")
end_of_url = src[:index]
我现在如何反转搜索,直到我点击网址的开头并获得//
的开始索引?
预期输出为:
//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=645&q=85&auto=format&sharp=10&s=19e251fb9bd21787e8364c091e4a89ce
答案 0 :(得分:0)
您可以使用str
转换x[::-1]
。如果您在已翻译的字符串中搜索转动的图案,您将按所需的顺序查找出现的图案。请记住在使用之前转换索引值。
答案 1 :(得分:0)
您可以使用string.rfind()
函数,它以相反的顺序搜索字符串。
示例 -
>>> if ' 645w' in src:
... index = src.find(' 645w')
... end_of_url = src[:index]
... print(end_of_url[end_of_url.rfind('//'):])
...
//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=645&q=85&auto=format&sharp=10&s=19e251fb9bd21787e8364c091e4a89ce
答案 2 :(得分:0)
你可以用','分割,并在每个字符串上搜索645w。
示例:
>>> links = "a 620w, b 700w, c 645w, d 456w";
>>> linksArray = links.split(",");
>>> for link in linksArray:
... index = link.find("645w");
... if index > -1:
... print(link[:index]);
// out : " c "
你需要多调整一点,但我更喜欢保持友好而不是反向搜索和剪切。
答案 3 :(得分:0)
这将为您提供包含645w
[url[:url.find(' 645w')] for url in src.split(', ') if ' 645w' in url]
在这种情况下评估为:
['//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=645&q=85&auto=format&sharp=10&s=19e251fb9bd21787e8364c091e4a89ce']
答案 4 :(得分:0)
s = "//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=620&q=85&auto=format&sharp=10&s=ed7be5af3592322971e9e2276b5338f8 620w, //i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=700&q=85&auto=format&sharp=10&s=d49ff60c0c561cb2eee8d119acba62c5 700w, //i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=645&q=85&auto=format&sharp=10&s=19e251fb9bd21787e8364c091e4a89ce 645w, //i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=465&q=85&auto=format&sharp=10&s=fd5d66ed602638295446f28c4853744d 465w"
print([url.strip(" 645w") for url in s.split(",") if url.endswith("645w")])
['//i.guim.co.uk/img/media/d126680aca92ff4251536f788b60233b5463af89/1585_707_3685_2210/3685.jpg?w=645&q=85&auto=format&sharp=10&s=19e251fb9bd21787e8364c091e4a89ce']