import pywikibot, re

def extract_titles(text, beginning, match_title):
	titles = {
	-1: beginning
	}
	i = 0
	#print text
	if not text:
		return None
	while re.search(match_title, text):
		titles[i] = re.search(match_title, text).group(0)
		
		if titles[i][0] == '\n':
			titles[i] = titles[i][1:]
		
		#print titles[i]
		text = text[text.index(titles[i]) + len(titles[i]):]
		i += 1
	
	del titles[-1]
	
	return titles

def extract_sections(text, titles):
	if not titles:
		return None
		
	sections = {}
	
	for setion_number in titles:
		# Si jamais le titre est celui de la dernière section, on procède
		# sans rechercher le titre de la section suivante, puisqu'elle
		# n'existe pas.
		if (setion_number + 1) != len(titles):
			sections[setion_number] = text[text.index(titles[setion_number]):text.index(titles[setion_number + 1])]
			text = text[text.index(sections[setion_number]) + len(sections[setion_number]):]
		else:
			sections[setion_number] = text[text.index(titles[setion_number]):]
	
	return sections
	

def extract_sections_with_titles(text, beginning, match_title):
	titles = extract_titles(text, beginning, match_title)
	if not titles:
		return None
	
	sections = {}
	
	for setion_number in titles:
		# Si jamais le titre est celui de la dernière section, on procède
		# sans rechercher le titre de la section suivante, puisqu'elle
		# n'existe pas.
		current_title = titles[setion_number]
		if (setion_number + 1) != len(titles):
			sections[current_title] = text[text.index(titles[setion_number]):text.index(titles[setion_number + 1])]
			text = text[text.index(sections[current_title]) + len(sections[current_title]):]
		else:
			sections[current_title] = text[text.index(titles[setion_number]):]
		
	
	return sections
p = pywikibot.Page(pywikibot.Site('fr'), 'Discussion_utilisateur:Otourly')
    
titres = extract_titles(p.text, beginning = '</div></div>', match_title = re.compile(u"(\n|^)== *([^=].+[^=]) *=="))
sections = extract_sections(p.text, titres)
WARNING: API warning (main): Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes. Use [[Special:ApiFeatureUsage]] to see usage of deprecated features by your application.
WARNING: API warning (revisions): Because "rvslots" was not specified, a legacy format has been used for the output. This format is deprecated, and in the future the new format will always be used.
a = pywikibot.Page(pywikibot.Site('fr'), 'Discussion_utilisateur:Otourly/Archive_9')
a.text = ''.join(list(sections.values())[:100])
a.save('archivage depuis [[Discussion_utilisateur:Otourly]]')

p.put(p.text.replace(a.text, ''), 'archivage vers [[%s]]' % a.title())
Page [[fr:Discussion utilisateur:Otourly/Archive 9]] saved
WARNING: API warning (main): Subscribe to the mediawiki-api-announce mailing list at <https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce> for notice of API deprecations and breaking changes. Use [[Special:ApiFeatureUsage]] to see usage of deprecated features by your application.
WARNING: API warning (revisions): Because "rvslots" was not specified, a legacy format has been used for the output. This format is deprecated, and in the future the new format will always be used.
Page [[fr:Discussion utilisateur:Otourly]] saved