import time from bs4 import BeautifulSoup from selenium import webdriver from…
- User
import time from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys
def get_token_info(token_symbol): chrome_options = Options() chrome_options.add_argument("–headless")
# Update the path below to the actual path to your WebDriver executable service = Service( executable_path="C:\\Users\\AshotxXx\\PycharmProjects\\Parcing\\pythonProject\\ChromeDriver\\chromedriver.exe") driver = webdriver.Chrome(service=service, options=chrome_options) driver.get("https://moonarch.app/") try: time.sleep(1) WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'form-control'))) # Find the search box and interact with it search_box = driver.find_element(By.CLASS_NAME, 'form-control') search_box.click() search_box.send_keys(token_symbol) search_box.send_keys(Keys.ENTER) time.sleep(1) # Wait for the token info and infocard elements to appear after entering the symbol WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "token-info"))) WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "infocard"))) # Extract information using BeautifulSoup soup = BeautifulSoup(driver.page_source, "html.parser") token_info_element = soup.find("div", class_="token-info") infocard_element = soup.find("div", class_="infocard") check_alert_element = soup.find("div", class_="token-check-message check-alert") check_warning_element = soup.find("div", class_="token-check-message check-warning") check_info_element = soup.find("div", class_="token-check-message check-info") not_verified_element = soup.find("div", class_="not-verified") check_alert_status = "Yes" if check_alert_element else "None" check_warning_status = "Yes" if check_warning_element else "None" check_info_status = "Yes" if check_info_element else "None" not_verified_status = "Yes" if not_verified_element else "None" if token_info_element and infocard_element: token_name_element = token_info_element.find("span", class_="name") token_symbol_element = token_info_element.find("span", class_="symbol") info_items = infocard_element.find_all("li") if token_name_element and token_symbol_element and len(info_items) >= 7: token_name = token_name_element.text.strip() token_symbol = token_symbol_element.text.strip() price = info_items[0].find("span", class_="value").text.strip() max_supply = info_items[1].find("span", class_="value").text.strip() market_cap = info_items[2].find("span", class_="value").text.strip() liquidity = info_items[3].find("span", class_="value").text.strip() liq_mc = info_items[4].find("span", class_="value").text.strip() token_age = info_items[6].find("span", class_="value").text.strip() driver.quit() return { "name": token_name, "symbol": token_symbol, "price": price, "max_supply": max_supply, "market_cap": market_cap, "liquidity": liquidity, "liq_mc": liq_mc, "token_age": token_age, "check_alert": check_alert_status, "check_warning": check_warning_status, "check_info": check_info_status, "not_verified": not_verified_status } else: driver.quit() error_info = f"token_name_element: {token_name_element}, token_symbol_element: {token_symbol_element}, info_items count: {len(info_items)}" return {"error": f"Failed to find the required info items or name and.symbol. Error_info: {error_info}"} else: driver.quit() return {"error": f"Failed to find the required elements for token_info_element ({token_info_element}) and infocard_element ({infocard_element})."} except Exception as e: driver.quit() return {"error": f"Failed to find the required elements or error occurred: {e}"} - Assistant
Yes, you can achieve the same output without using Selenium and WebDriver by using the
requestslibrary to send HTTP requests and BeautifulSoup to parse the HTML content. However, it is important to note that therequestsmethod may not work if the website relies on JavaScript to load content or interact with the page. In that case, Selenium might be the better option.Here's how you can get the token information using
requests:import requests from bs4 import BeautifulSoup def get_token_info(token_symbol): url = f"https://moonarch.app/?t={token_symbol}" response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, "html.parser") # Extract information using BeautifulSoup (same as before) # ... return { # ... } else: return {"error": f"Failed to fetch page, status code: {response.status_code}"} # Rest of the code...Please note that using this method might not work if the website relies on JavaScript to load the content or interact with the user. In such cases, Selenium is a better option.
- User
Write full code
Is this the best option or is there a better one? If there is, tell me which one
- Assistant
- User
- Assistant
- User
- Assistant
First, you need to install the Playwright library and the necessary browser dependencies:
pip install playwright playwright installHere's the modified code using Playwright:
import asyncio from bs4 import BeautifulSoup from playwright.async_api import async_playwright async def get_token_info(token_symbol): async with async_playwright() as p: browser = await p.chromium.launch(headless=True) context = await browser.new_context() page = await context.new_page() await page.goto("https://moonarch.app/") search_box_selector = 'input.form-control' await page.wait_for_selector(search_box_selector) search_box = await page.query_selector(search_box_selector) await search_box.type(token_symbol) await search_box.press('Enter') await page.wait_for_selector('.token-info') await page.wait_for_selector('.infocard') soup = BeautifulSoup(await page.content(), "html.parser") # Extract information using BeautifulSoup (same as before) # ... await browser.close() return { # ... } token_symbols = [ "0x0E481Fa712201f61dAf97017138Efaa69e2A3df3", "0x2023aa62A7570fFd59F13fdE2Cac0527D45abF91", "0x2222222222222222222222222222222222222222", "0x0d4890ecEc59cd55D640d36f7acc6F7F512Fdb6e" # Add more token symbols here… ] async def main(): while True: result_tasks = [get_token_info(token_symbol) for token_symbol in token_symbols] results = await asyncio.gather(*result_tasks) print(results) await asyncio.sleep(60) asyncio.run(main()) - User
Write full code
- Assistant