import requests from bs4 import BeautifulSoup def fetchandparse(url): response =…
- User
import requests from bs4 import BeautifulSoup
def fetch_and_parse(url): response = requests.get(url)
if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parse") return soupdef extract_moonarch_data(soup): # Extract the token title from the ‘h1’ tag token_title = soup.find("h1").text.strip() print(f"Token Title: {token_title}")
# Extract the token description from the ‘h3’ tag token_description = soup.find("h3").text.strip() print(f"Token Description: {token_description}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}")if name == "main": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup) The code above displays information
C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\venv\Scripts\python.exe C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\main.py
Process finished with exit code 0 it returned the name and symbol of the token.
These are the items in the Devtools tab
- Assistant
The code seems to be incomplete, and it looks like it doesn't run correctly because the "if name == "main":" condition should be "if name == "main":". Also, "html.parse" should be "html.parser". Here's the corrected code:
import requests from bs4 import BeautifulSoup def fetch_and_parse(url): response = requests.get(url) if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parser") return soup def extract_moonarch_data(soup): # Extract the token title from the ‘h1’ tag token_title = soup.find("h1").text.strip() print(f"Token Title: {token_title}") # Extract the token description from the ‘h3’ tag token_description = soup.find("h3").text.strip() print(f"Token Description: {token_description}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}") if __name__ == "__main__": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup) - User
- Assistant
It seems like you've pasted the code again with some changes in quotation marks, which may be causing errors when running the code. Here's the corrected version of the code with proper indentation and quotation marks:
import requests from bs4 import BeautifulSoup def fetch_and_parse(url): response = requests.get(url) if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parser") return soup def extract_moonarch_data(soup): # Extract the token title from the ‘h1’ tag token_title = soup.find("h1").text.strip() print(f"Token Title: {token_title}") # Extract the token description from the ‘h3’ tag token_description = soup.find("h3").text.strip() print(f"Token Description: {token_description}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}") if __name__ == "__main__": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup) - User
C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\venv\Scripts\python.exe C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\main.py Traceback (most recent call last): File "C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\main.py", line 34, in if name == "main": ^^^^ NameError: name 'name' is not defined
Process finished with exit code 1
Fix it
- Assistant
Apologies for the oversight. Please replace
if name == "main":withif __name__ == "__main__":. Here is the corrected code:import requests from bs4 import BeautifulSoup def fetch_and_parse(url): response = requests.get(url) if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parser") return soup def extract_moonarch_data(soup): # Extract the token title from the ‘h1’ tag token_title = soup.find("h1").text.strip() print(f"Token Title: {token_title}") # Extract the token description from the ‘h3’ tag token_description = soup.find("h3").text.strip() print(f"Token Description: {token_description}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}") if __name__ == "__main__": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup) - User
C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\venv\Scripts\python.exe C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\main.py Traceback (most recent call last): File "C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\main.py", line 38, in extract_moonarch_data(soup) File "C:\Users\AshotxXx\PycharmProjects\Parcing\pythonProject7\main.py", line 18, in extract_moonarch_data token_title = soup.find("h1").text.strip() ^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'text'
Process finished with exit code 1
Fix it
- Assistant
It appears that the "h1" tag was not found in the soup object, causing the AttributeError. Since the website structure might have changed, let's update the parsing with different tag selectors. Please find the updated code below:
import requests from bs4 import BeautifulSoup def fetch_and_parse(url): response = requests.get(url) if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parser") return soup def extract_moonarch_data(soup): token_info = soup.find("div", class_="mt-4 mb-5 text-center") if token_info: # Extract the token title and token description token_title, token_description = token_info.text.strip().split("\n") print(f"Token Title: {token_title}") print(f"Token Description: {token_description}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}") if __name__ == "__main__": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup) - User
This code does not return the name and symbol of the token
Fix this by referring to the page code below
- Assistant
I apologize for the confusion. Based on the provided page code, we can extract the token name and symbol using the "span" tags with classes "name" and "symbol". Here's the updated code:
import requests from bs4 import BeautifulSoup def fetch_and_parse(url): response = requests.get(url) if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parser") return soup def extract_moonarch_data(soup): token_name = soup.find("span", class_="name") token_symbol = soup.find("span", class_="symbol") if token_name and token_symbol: print(f"Token Name: {token_name.text.strip()}") print(f"Token Symbol: {token_symbol.text.strip()}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}") if __name__ == "__main__": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup) - User
- Assistant
I apologize for the inconvenience. Based on the provided page code, here's the updated code to extract the token name and symbol using the "span" tags with classes "name" and "symbol":
import requests from bs4 import BeautifulSoup def fetch_and_parse(url): response = requests.get(url) if response.status_code == 200: page_content = response.text else: print(f"Error: Unable to fetch the page. Status code: {response.status_code}") return soup = BeautifulSoup(page_content, "html.parser") return soup def extract_moonarch_data(soup): token_name = soup.find("span", class_="name") token_symbol = soup.find("span", class_="symbol") if token_name and token_symbol: print(f"Token Name: {token_name['title']}") print(f"Token Symbol: {token_symbol['title']}") # Locate the text elements containing token data token_data_elements = soup.find_all("div", class_="col-sm-6 col-md-3") # Extract and display the token data for element in token_data_elements: key = element.find("small").text.strip() value = element.find(class_="font-weight-bold").text.strip() print(f"{key}: {value}") if __name__ == "__main__": url = "https://moonarch.app/token/0x70D6a53C38cD80935715c304535A0885cCe35db7" soup = fetch_and_parse(url) if soup: extract_moonarch_data(soup)