As you iterate over all the <a> elements, you can use the str.endswith() function to check if the URL ends with .img.html. Do this to extract all your URLs to a single list:
urls = []
for link in soup.find_all('a'):
url = link.get('href')
if url.endswith('.img.html')
urls.append(url)
Which gives a list like so:
urls = ['/gauguin/twrp-3.5.2_10-0-gauguin.img.html',
'/gauguin/twrp-3.5.1_10-0-gauguin.img.html',
'/gauguin/twrp-3.5.0_10-0-gauguin.img.html']
Next, it depends on whether the version-specifiers in your URLs are guaranteed to follow lexicographic order, i.e. will doing a simple string compare get you the latest one? This is usually true if they're all of the same format where the number of digits in each part of the version number remains the same for different strings. The ones you have shown in your example meet this requirement.
If this is the case, simple do
max(urls)
which gives
'/gauguin/twrp-3.5.2_10-0-gauguin.img.html'
If this is not the case, (for example if you had '/gauguin/twrp-3.15.2_10-0-gauguin.img.html', which is numerically > 3.5.2 but not lexicographically) you're going to have to parse out the version number from your string, possibly using a regex, and compare those version numbers. You can do this using the key argument to the max() function (stay tuned, I'm editing my answer for this).
Let's say your version numbers have the format <numbers>.<numbers>.<numbers>_<numbers>-<numbers>. You'd use the following regex (try it online):
\d+\.\d+\.\d+_\d+-\d+
Explanation
\d+ : One or more digits
\. : The . character
To use it with max(), you could write a function that extracts the version number from the file name:
import re
def extract_version(filename):
# e.g. filename = '/gauguin/twrp-3.5.2_10-0-gauguin.img.html'
match = re.search(r"(\d+)\.(\d+)\.(\d+)_(\d+)-(\d+)", filename)
# e.g. match = <re.Match object; span=(14, 24), match='3.5.2_10-0'>
# e.g. match.groups() = ('3', '5', '2', '10', '0')
if match is not None:
return tuple(int(m) for m in match.groups()) # Convert match to tuple of integer for correct comparison
return tuple() # if match is none, return an empty tuple
The one modification I made to the regex was to surround the \d+ in parentheses. The parentheses make it a capturing group, so all the numeric parts are captured as separate groups. Then, re.search() returns a match object, and its .groups() method gives a tuple containing the numbers, but as strings. So we need to convert these strings to integers before returning it.
Then, use the function as the key argument to max():
max(urls, key=extract_version)