<html lang="en-us"> <head> <!-- Header --> </head> <body class=""> <div id="container">…
- User
import nest_asyncio import asyncio import aiohttp from bs4 import BeautifulSoup
Allow nested event loops in Colab
nest_asyncio.apply()
async def fetch_decompose_into_aiohttp(material): url = f"https://oqmd.org/materials/composition/{material}" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " "AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/58.0.3029.110 Safari/537.3" } async with aiohttp.ClientSession(headers=headers) as session: try: async with session.get(url) as response: if response.status != 200: return f"Failed to fetch data: Status {response.status}" html = await response.text() soup = BeautifulSoup(html, 'html.parser')
# Find the "Decompose Into" section decompose_label = soup.find('b', text="Decompose Into:") if not decompose_label: return "Decompose Into information not found." # Find the parent <p> tag containing the decomposition links decompose_content = decompose_label.find_next("p", class_="comp") if not decompose_content: return "Decompose Into information is unavailable in the expected format." # Extract decomposition phases phases = [] for a_tag in decompose_content.find_all("a"): phase_name = ''.join([sub.text if sub.name == 'sub' else sub for sub in a_tag.contents]) phase_link = a_tag['href'] phases.append({"name": phase_name.strip(), "link": f"https://oqmd.org{phase_link}"}) return phases except Exception as e: return f"An error occurred: {str(e)}"Execute the async function using an event loop
material = "NaLiCu2S2" # Replace with desired material composition decompose_info = asyncio.get_event_loop().run_until_complete(fetch_decompose_into_aiohttp(material))
Print the extracted decomposition information
if isinstance(decompose_info, list): print(f"Decompose Into Phases for {material}:") for phase in decompose_info: print(f" - {phase['name']}: {phase['link']}") else: print(decompose_info)
print(f"Decompose Into Phases for {material}:")import nest_asyncio import asyncio import aiohttp from bs4 import BeautifulSoup
Allow nested event loops in Colab
nest_asyncio.apply()
async def fetch_decompose_into_aiohttp(material): url = f"https://oqmd.org/materials/composition/{material}" … print(f"Decompose Into Phases for {material}:") for phase in decompose_info: print(f" - {phase['name']}: {phase['link']}") else: print(decompose_info)
- Assistant