[英]Variable inside a search pattern in python
I have a soup from BeautifulSoup and I have a list of keys (key_image) which value or values I want to introduce in another list. 我有BeautifulSoup的汤,并且有一个键列表(key_image),我想在另一个列表中引入一个或多个值。
soup = BeautifulSoup(stringapplet)
key_image = ['archivo=', 'imagen=', 'image=', 'imaxe=', 'arquivo=']
imageslist = []
This way I get the list of values for archivo : 这样我就可以得到archivo的值列表:
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))
good, but... I need in imageslist also searches of "patrons" like archivo : of imagen , of imaxe ... etc. so I suppose I need to introduce a variable in a search pattern, then I do: 不错,不过......我需要imageslist也搜索的“食客”像档案馆 :imagen画质的,imaxe的......等等,所以我想我需要在搜索模式引入一个变量,然后我做的:
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))
but imageslist is empty because the last line is never loaded. 但imageslist为空,因为从不加载最后一行。 ¿? ¿?
I do patron.pattern and I get r"archivo='([\\w./]+)'" also, so what is wrong? 我做patron.pattern ,也得到r“ archivo ='([\\ w./]+)'” ,那怎么了?
How can I do the search of all values of all key_images values? 如何搜索所有key_images值的所有值?
Thanks. 谢谢。
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.