re.findall(pattern, string, flags=0)
返回string中所有非重叠的匹配——字符串列表
Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.
使用:
url_str = """{
'keyword=AAAAAAE5%9C%9F1&select',
'keyword=E5%A3%A4%E6%9C0&select',
'keyword=%E6%9C%BA%E6%A2&select',
'keyword=4%E6%88%C%BA%E6&select',
}"""
pattern = re.compile(r"keyword=([^&]+)&select")
matches = re.findall(pattern, url_str)
# 下面一行效果同上面两行
# matches = re.compile(r"keyword=([^&]+)&select").findall(url_str)
结果:['AAAAAAE5%9C%9F1', 'E5%A3%A4%E6%9C0', '%E6%9C%BA%E6%A2', '4%E6%88%C%BA%E6']