找到正则表达式来获取两个短语之间的URL

时间:2018-01-18 09:29:01

标签: python regex

我有以下脚本尝试获取此网址:https://clips-media-assets.twitch.tv/178569498.mp4介于{“quality”:“1080”,“source”:“和a”之间,但我的正则表达式似乎不起作用

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)

1 个答案:

答案 0 :(得分:4)

如果你坚持使用正则表达式来解决这个问题,请试试这个(如图here所示):

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

我不是特别知道这个案例,但我必须提一下,通常用正则表达式解析JSON并不理想。当然,您可以使用( *)向正则表达式添加动态编号的空格,但我认为最好使用JSON解析器。