Open AmwnehZarei opened 5 months ago
import requests import json from bs4 import BeautifulSoup
if name == 'main': response = requests.get( url='https://sahmeto.com/crypto-sitemap.xml' ) xml_data = response.text soup = BeautifulSoup(xml_data, 'xml') # Convert XML to dictionary data_dict = {} for tag in soup.find_all(True): tag_name = tag.name if tag.contents: if len(tag.contents) > 1: data_dict[tag_name] = [child.name for child in tag.findChildren()] else: data_dict[tag_name] = tag.contents[0] else: data_dict[tag_name] = None # Convert dictionary to JSON json_data = json.dumps(data_dict, indent=4) print(json_data)
تمرین
import requests import json from bs4 import BeautifulSoup
if name == 'main': response = requests.get( url='https://sahmeto.com/crypto-sitemap.xml' ) xml_data = response.text soup = BeautifulSoup(xml_data, 'xml') # Convert XML to dictionary data_dict = {} for tag in soup.find_all(True): tag_name = tag.name if tag.contents: if len(tag.contents) > 1: data_dict[tag_name] = [child.name for child in tag.findChildren()] else: data_dict[tag_name] = tag.contents[0] else: data_dict[tag_name] = None # Convert dictionary to JSON json_data = json.dumps(data_dict, indent=4) print(json_data)