Web graph
To create and visualize a web graph from a website URL, you can follow these general steps:
import requests
from bs4 import BeautifulSoup
import networkx as nx
import matplotlib.pyplot as plt
def create_and_visualize_web_graph(url):
# Web scraping to extract links
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
links = [a['href'] for a in soup.find_all('a', href=True)]
# Build graph
G = nx.Graph()
for link in links:
G.add_edge(url, link)
# Visualization
pos = nx.spring_layout(G) # Define layout
node_sizes = [10 * G.degree(node) for node in G.nodes]
nx.draw(G, pos, with_labels=True, font_weight='bold', node_size=node_sizes, node_color='skyblue', font_size=8)
plt.show()
# Example usage
website_url = 'https://example.com'
create_and_visualize_web_graph(website_url)
Remember to replace 'https://example.com' with the URL of the website you want to visualize.
Also, remember to review the terms of service of the website you are scraping to ensure compliance with their policies, and be considerate of the server load by adding delays between requests to avoid being blocked.