I am trying to web scrapping this Brazilian Law in a sctructured format like: {"artigo":int, "texto":str} *(article and text) The problem is the HTML of this page is terrible because, for example, only the first 199 articles have a class named "Artigo" and between some articles are some centered non-articles texts. So the best I was able until now was to recover the first 199 articles: import requests import re import json from bs4 import BeautifulSoup import chardet url = "https://www.planalto.gov.br/ccivil_03/_ato2015-2018/2015/lei/L13105compilada.htm" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" } response = requests.get(url, headers=headers) data = response.content detected = chardet.detect(data) encoding = detected['encoding'] print(f"Codificação detectada: {encoding}") html = data.decode(encoding) soup = BeautifulSoup(html, 'html.parser') artigos = [] for p in soup.find_all('p', class_='Artigo'): texto_completo = p.get_text(separator=' ', strip=True) match = re.search(r'Art\.\s*(\d+)', texto_completo) if match: num_artigo = int(match.group(1)) texto_limpo = re.sub(r'Art\.\s*\d+[ºº]?\s*', '', texto_completo, count=1) artigos.append({ "artigo": num_artigo, "texto": texto_limpo }) print(json.dumps(artigos, indent=2, ensure_ascii=False)) another problem is there a some articles with legal paragraphs (§): Art. 3º Não se excluirá da apreciação jurisdicional ameaça ou lesão a direito. § 1º É permitida a arbitragem, na forma da lei. § 2º O Estado promoverá, sempre que possível, a solução consensual dos conflitos. § 3º A conciliação, a mediação e outros métodos de solução consensual de conflitos deverão ser estimulados por juízes, advogados, defensores públicos e membros do Ministério Público, inclusive no curso do processo judicial. and I also need to include than as part of their texts So Is there a way to get correctly all articles ? Continue reading...