正则表达式不返回特定匹配

时间:2016-09-08 14:19:23

标签: regex python-3.x beautifulsoup python-requests

我正在尝试从网站上的脚本标记中提取链接。 目前我的正则表达式由于某种原因返回整个块..

这是我想要获取链接的脚本标记的内容:

<script type="text/javascript">
var key = '';
var url = 'http://stream1.song365.me/h1/20160129/1772422101/The%20Beatles%20-%20There%27s%20a%20Place%20%28Studio%20Outtake%20Takes%205%20%26%206%29_(song365.cc).mp3';
var hqurl = 'http://stream1.song365.me/h1/20160129/1772422101/The%20Beatles%20-%20There%27s%20a%20Place%20%28Studio%20Outtake%20Takes%205%20%26%206%29_(song365.cc).mp3';
$(document).ready(function(){
    $("div[rel='digg']").click(function(){
        var method = $(this).attr("method");
        var v = parseInt($(this).find('em').html());
        var p = this;
        $.post("/track/digg/2788951/" + method, function(data){
            if(data.status==0)
            {
                alert("today you have been digg it!")
            }
            else
            {
                $(p).find('em').html(data.number);
            }
        }, "JSON")
    })
    if(url.length!=0)
    {
        $("#download-link").attr("href", url + "?key=" + key).css("display","");;
    }
    if(hqurl.length!=0)
    {
        $("#download-link-hq").attr("href", hqurl + "?key=" + key).css("display","");
    }
});
</script>

这是我目前的代码:

request = requests.get(dummy_link)
    data = request.text
    soup = BeautifulSoup(data, 'html.parser')
    link = soup.findAll(text=re.compile('var hqurl.*?mp3'))

它返回整个脚本标记,但我只想将链接分配给hqurl变量。

@alecxe的帮助下的当前代码:

request = requests.get('https://www.song365mp3.biz/download/the-beatles-there039s-a-place-studio-outtake-takes-5-amp-6-2788951.html')
    data = request.text
    soup = BeautifulSoup(data, 'html.parser')
    pattern = re.compile("var hqurl = '(.*?mp3)';$", re.MULTILINE | re.DOTALL)
    link = soup.find("script", text=pattern)
    print(pattern.search(link.text).group(1))

但抛出错误:

    print((link.text).group(1))
AttributeError: 'ResultSet' object has no attribute 'text'

2 个答案:

答案 0 :(得分:1)

预编译模式并重用以定位元素并提取链接:

pattern = re.compile("var hqurl = '(.*?mp3)';", re.MULTILINE | re.DOTALL)
link = soup.find("script", text=pattern)
print(pattern.search(link.text).group(1))

请注意,我改进了表达式并添加了一个捕获组,该组将保存组中的实际链接,然后我们通过.group(1)访问该组。

打印:

http://stream1.song365.me/h1/20160129/1772422101/The%20Beatles%20-%20There%27s%20a%20Place%20%28Studio%20Outtake%20Takes%205%20%26%206%29_(song365.cc).mp3

答案 1 :(得分:-1)

试试这个:link = soup.findAll(text=re.compile('var hqurl = (.*?mp3)'))

enter image description here

https://regex101.com/r/jX7xK7/1