简体   繁体   English

查找正则表达式以获取两个短语之间的网址

[英]finding the regex to get a url between two phrases

I have the following script trying to get this url: https://clips-media-assets.twitch.tv/178569498.mp4 which is in between {"quality":"1080","source":" and a " but my regex doesn't seem to be working 我有以下脚本尝试获取此网址: https : //clips-media-assets.twitch.tv/178569498.mp4 ,它介于{“ quality”:“ 1080”,“ source”:“和”但是我的正则表达式似乎不起作用

dt = """
<body>
    <script>jQuery(window).load(function () {
      setTimeout(function(){s
      }, 1000);quality_options: [{"quality":"1080","source":"https://clips-media-assets.twitch.tv/178569498.mp4","frame_rate":60},{"quality":"720","source":"https://clips-media-assets.twitch.tv/AT-178569498-1280x720.mp4","frame_rate":60},{"quality":"480","source":"https://clips-media-assets.twitch.tv/AT-178569498-854x480.mp4","frame_rate":30},{"quality":"360","source":"https://clips-media-assets.twitch.tv/AT-178569498-640x360.mp4","frame_rate":30}]

    });</script>
</body>
[download]  28.2x of 57.90MiB at  1.54MiB/s ETA 00:26 


"""



pattern = re.compile(r'(?:\G(?!\A)|quality\":\"1080\",\"source\":\")(?:(?!\").)*', re.MULTILINE | re.DOTALL)
clipHTML = BeautifulSoup(dt, "html.parser")

scripts = clipHTML.findAll(['script'])
for script in scripts:
    if script:
        match = pattern.search(script.text)
        if match:
            email = match.group(0)
            print(email)

If you insist on using a regex to solve this, try this one (as shown here ): 如果你坚持使用正则表达式来解决这个问题,尝试这一个(如图所示这里 ):

(?<=quality\":\"1080\",\"source\":\")[^\"]+(?=\")

I don't know specifically about this case, but I have to mention that in general it's not ideal to parse JSON with regular expressions. 我不了解这种情况,但我不得不提一提,通常来说,用正则表达式解析JSON并不理想。 Of course you can add dynamic-numbered spaces to the regex using ( *) , but still I think it's better to use a JSON parser. 当然,您可以使用( *)向正则表达式添加动态编号的空格,但我仍然认为使用JSON解析器更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM