繁体   English   中英

python中搜索模式内的变量

[英]Variable inside a search pattern in python

我有BeautifulSoup的汤,并且有一个键列表(key_image),我想在另一个列表中引入一个或多个值。

    soup = BeautifulSoup(stringapplet)
    key_image = ['archivo=', 'imagen=', 'image=', 'imaxe=', 'arquivo=']
    imageslist = []

: 这样我就可以得到的值列表:

    patron = re.compile(r"archivo='([\w\./]+)'")
    for tag in soup.findAll('param'):
        if patron.search(tag['value']):
            imageslist.append(patron.search(tag['value']).group(1))

also searches of like : of , of ... etc. so I suppose I need to introduce a variable in a search pattern, then I do: 不错,不过......我需要也搜索的 的......等等,所以我想我需要在搜索模式引入一个变量,然后我做的:

    for x in key_image:
        if string.find(stringapplet, x) != -1:
            expression = 'r' + '"' + x + "'" + '([\w\./]+)' + "'" + '"'
            patron = re.compile(expression)
            for tag in soup.findAll('param'):
                if patron.search(tag['value']):
                    imageslist.append(patron.search(tag['value']).group(1))

is empty because the last line is never loaded. 为空,因为从不加载最后一行。 ¿?

and I get also, so what is wrong? 我做 ,也得到 ,那怎么了?

如何搜索所有key_images值的所有值?

谢谢。

for x in key_image:
    if string.find(stringapplet, x) != -1:
        expression = r"%s'([\w\./]+)'" % x
        patron = re.compile(expression)
        for tag in soup.findAll('param'):
            result = patron.search(tag['value'])
            if result:
                imageslist.append(result.group(1))

暂无
暂无

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

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