Q:

Explanation of the use of this command (Beautiful soup and re.compile)

I have the following command which I am struggling to break down and explain for teaching purposes.
images = bs.find_all('img', {'src':re.compile('.jpg')})

The whole code for this function is:
def imagescrape():
    result_images=[]
    html = urlopen('https://en.wikipedia.org/wiki/Rembrandt')
    bs = BeautifulSoup(html, 'html.parser')
    images = bs.find_all('img', {'src':re.compile('.jpg')})
    for image in images:
        result_images.append("https:"+image['src']+'\n') #concatenation!
    return result_images

Of course, we know that images is the variable that stores the result of the command.
I also know the result of bs.find_all (bs being the object created and .find all is the method being used to find all instances of the 'img' tag).
The find_all method takes two arguments.  One is 'img' (this could be, correct me if wrong, any string). The second argument appears to be a dictionary and here I am slightly lost:
'src':re.compile('.jpg')}

From research, I understand the following:
"src" is the key in the dictionary
and the next part:
re.compile('.jpg')}

is the value part of the dictionary.
But why has a dictionary been used?
Also, more importantly, what, in this specific situation, is re.compile('jpg')} actually doing in this case? What is being returned? And why is it in a dictionary being used? I would need a very student-friendly beginner, broken down explanation please.

A:

You don't have to use a dictionary.
One of the following options can be used.
images = bs.find_all('img', {'src':re.compile('.jpg')})
Is the equivalent of the following, which uses named arguments.
images = bs.find_all('img', attrs={'src': re.compile('.jpg')})
images = bs.find_all('img', **{'src':re.compile('.jpg')})
Is identical to the following.
images = bs.find_all('img', src=re.compile('.jpg'))
The expression re.compile('.jpg') returns a regular expression object, which can be used several times or later. It's a modular approach to regular expressions.
Keep in mind that some characters have a deeper meaning within regular expressions. In this case this concerns the character '.'. I think you should use '\.jpg'. Otherwise, it would match the meaning of any character followed by jpg.

