from bs4 import BeautifulSoup
import json
import re
with open('example.html', 'r') as file:
html_str = file.read()
soup = BeautifulSoup(html_str, 'html.parser')
h1_title = soup.find('h1').get_text()
script = json.loads(soup.find('script', {'id': 'article'}).get_text())
markdown = script.get('body')
pattern = re.compile(r'\[\s*`?(.*?)`?\s*]\((.*?)\)')
tools = [{"name": text, "link": link} for text, link in pattern.findall(markdown)]
match = re.search(r"json\s*(.*?)```\s*", markdown, re.DOTALL)
random_data = json.loads(match.group(1)).get('randomData') if match else {}
tags = script.get('tags', [])
json_data = {
"heading": h1_title,
"article": {
"title": script.get('title', 'No Title Found'),
"body": {
"tools": tools,
"moreData": random_data
},
"tags": tags
}
}
json_str = json.dumps(json_data, indent=4)
print(json_str)
Respondendo a "Você chegou a fazer o código ou só fez um levan..." dentro da publicação [DESAFIO] Utilize técinicas de Web Scraping para Extrair Dados
2