面试题答案
一键面试import re
def extract_links(html):
pattern = r'<a href="(.*?)">(.*?)</a>'
matches = re.findall(pattern, html)
result = [{"url": match[0], "text": match[1]} for match in matches]
return result
你可以使用以下方式调用这个函数:
html_text = '<a href="http://example1.com">link text1</a><a href="http://example2.com">link text2</a>'
print(extract_links(html_text))