import re
from pprint import pprint
text = ('You should use index-url = https://pypi.python.org/simple rather than'
'find-links in your config. This will replace the default rather than just '
'adding another option (which is what find-links does '
'http://pypi.python.org/master ).')
links = {link.rpartition('/')[-1]: link for link in re.findall(r'https?://\S+', text)}
links2 = [{'link': link, 'id': link.rpartition('/')[-1]} for link in re.findall(r'https?://\S+', text)]
pprint(links, indent=1)
pprint(links2, indent=1)
for item in links2:
print(item.get('link'))
for item in links2:
print(item.get('id'))
# {'master': 'http://pypi.python.org/master',
# 'simple': 'https://pypi.python.org/simple'}
# [{'id': 'simple', 'link': 'https://pypi.python.org/simple'},
# {'id': 'master', 'link': 'http://pypi.python.org/master'}]
# https://pypi.python.org/simple
# http://pypi.python.org/master
# simple
# master
В данном случае используется библиотека для работы с регулярными выражениями re
для которой задается шаблон поиска, где искомая комбинация начинается с http
после которой возможно будет s
далее следуют ://
после которых неразрывная комбинация любых не пробельных символов. Метод findall находит все комбинации описанные шаблоном.
Затем в процессе поиска при помощи метода строки rpartition
мы получаем по разделителю '/' каждую ссылку на кортеж из 3х элементов, к примеру из https://pypi.python.org/simple
мы получим ('https://pypi.python.org', '/', 'simple')
тоесть разбиваем по самому правому разделителю. Все это обернуто в генератор словаря (это я уже от себя добавил, мне так показалось юзабильней)