Spaces:
Runtime error
Runtime error
File size: 14,239 Bytes
1744e85 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | import requests
import textstat
from bs4 import BeautifulSoup
import json
import re
from typing import Dict
class WebScraper:
"""Handles web scraping and content extraction."""
@staticmethod
def scrape_url(url: str, timeout: int = 10) -> Dict:
"""
Scrapes a URL and extracts basic content and metadata.
Args:
url (str): The URL to scrape
timeout (int): Request timeout in seconds
Returns:
Dict: Contains text_content, meta_title, meta_description, links, etc.
"""
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
response = requests.get(url, timeout=timeout, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'lxml')
# Extract main content
main_content_area = soup.find('article') or soup.find('main') or soup.body
if main_content_area:
text_content = main_content_area.get_text(separator=' ', strip=True)
else:
text_content = soup.get_text(separator=' ', strip=True)
# Extract metadata
meta_title = soup.find('title').get_text(strip=True) if soup.find('title') else "Not found"
meta_desc_tag = soup.find('meta', attrs={'name': 'description'})
meta_description = meta_desc_tag['content'] if meta_desc_tag and 'content' in meta_desc_tag.attrs else "Not found"
# Extract all links
all_links = [a['href'] for a in soup.find_all('a', href=True)]
return {
"status": "success",
"text_content": text_content,
"meta_title": meta_title,
"meta_description": meta_description,
"all_links": all_links,
"content_length": len(text_content)
}
except requests.exceptions.RequestException as e:
return {"status": "failed", "error": f"Scraping failed: {str(e)}"}
except Exception as e:
return {"status": "failed", "error": f"Error during scraping/parsing: {str(e)}"}
class SEOAnalyzer:
"""Handles SEO-related analysis and metrics."""
@staticmethod
def analyze_seo_metrics(scraped_data: Dict, url: str) -> Dict:
"""
Analyzes SEO metrics from scraped data.
Args:
scraped_data (Dict): Output from WebScraper.scrape_url()
url (str): The original URL for link analysis
Returns:
Dict: SEO metrics including link analysis, meta tag analysis
"""
if scraped_data["status"] != "success":
return {"error": "Cannot analyze SEO metrics - scraping failed"}
try:
all_links = scraped_data.get("all_links", [])
# Categorize links
internal_links = len([link for link in all_links if url in link or link.startswith('/')])
external_links = len([link for link in all_links if url not in link and link.startswith('http')])
# Analyze meta tags
meta_title = scraped_data.get("meta_title", "")
meta_description = scraped_data.get("meta_description", "")
return {
"meta_title": meta_title,
"meta_description": meta_description,
"meta_title_length": len(meta_title),
"meta_description_length": len(meta_description),
"internal_links": internal_links,
"external_links": external_links,
"total_links": len(all_links),
"has_meta_description": meta_description != "Not found",
"title_seo_friendly": 30 <= len(meta_title) <= 60,
"description_seo_friendly": 120 <= len(meta_description) <= 160
}
except Exception as e:
return {"error": f"SEO analysis failed: {str(e)}"}
class ReadabilityAnalyzer:
"""Handles text readability and statistical analysis."""
@staticmethod
def analyze_readability(text_content: str) -> Dict:
"""
Analyzes text readability using various metrics.
Args:
text_content (str): The text content to analyze
Returns:
Dict: Readability metrics and statistics
"""
if not text_content:
return {"error": "No text content to analyze"}
try:
word_count = textstat.lexicon_count(text_content)
sentence_count = textstat.sentence_count(text_content)
return {
"word_count": word_count,
"sentence_count": sentence_count,
"character_count": len(text_content),
"paragraph_count": len([p for p in text_content.split('\n\n') if p.strip()]),
"average_words_per_sentence": round(word_count / sentence_count, 2) if sentence_count > 0 else 0,
"flesch_reading_ease": textstat.flesch_reading_ease(text_content),
"flesch_kincaid_grade": textstat.flesch_kincaid_grade(text_content),
"gunning_fog": textstat.gunning_fog(text_content),
"coleman_liau_index": textstat.coleman_liau_index(text_content),
"automated_readability_index": textstat.automated_readability_index(text_content),
"estimated_reading_time_minutes": round(word_count / 200, 2) if word_count > 0 else 0,
"reading_difficulty": ReadabilityAnalyzer._get_reading_difficulty(textstat.flesch_reading_ease(text_content))
}
except Exception as e:
return {"error": f"Readability analysis failed: {str(e)}"}
@staticmethod
def _get_reading_difficulty(flesch_score: float) -> str:
"""Convert Flesch Reading Ease score to difficulty level."""
if flesch_score >= 90:
return "Very Easy"
elif flesch_score >= 80:
return "Easy"
elif flesch_score >= 70:
return "Fairly Easy"
elif flesch_score >= 60:
return "Standard"
elif flesch_score >= 50:
return "Fairly Difficult"
elif flesch_score >= 30:
return "Difficult"
else:
return "Very Difficult"
class ContentAnalyzer:
"""Handles content structure and pattern analysis."""
@staticmethod
def analyze_content_structure(text_content: str, scraped_data: Dict) -> Dict:
"""
Analyzes content structure and patterns.
Args:
text_content (str): The text content to analyze
scraped_data (Dict): Scraped data containing HTML structure info
Returns:
Dict: Content structure analysis
"""
if not text_content:
return {"error": "No text content to analyze"}
try:
# Basic content analysis
sentences = text_content.split('.')
paragraphs = [p.strip() for p in text_content.split('\n\n') if p.strip()]
# Find potential CTAs (basic pattern matching)
cta_patterns = [
r'\b(click here|learn more|get started|sign up|buy now|download|subscribe|contact us)\b',
r'\b(try free|free trial|book now|shop now|order now|get quote)\b'
]
potential_ctas = []
for pattern in cta_patterns:
matches = re.findall(pattern, text_content, re.IGNORECASE)
potential_ctas.extend(matches)
# Analyze content patterns
has_questions = '?' in text_content
has_lists = any(marker in text_content for marker in ['•', '*', '-', '1.', '2.'])
return {
"sentence_count": len([s for s in sentences if s.strip()]),
"paragraph_count": len(paragraphs),
"average_paragraph_length": sum(len(p.split()) for p in paragraphs) / len(paragraphs) if paragraphs else 0,
"has_questions": has_questions,
"has_lists": has_lists,
"potential_ctas": list(set(potential_ctas)),
"cta_count": len(set(potential_ctas)),
"content_density": len(text_content.split()) / max(len(paragraphs), 1),
"uppercase_ratio": sum(1 for c in text_content if c.isupper()) / len(text_content) if text_content else 0
}
except Exception as e:
return {"error": f"Content structure analysis failed: {str(e)}"}
class KeywordAnalyzer:
"""Handles basic keyword and phrase analysis."""
@staticmethod
def extract_basic_keywords(text_content: str, top_n: int = 10) -> Dict:
"""
Extracts basic keywords using frequency analysis.
Args:
text_content (str): The text content to analyze
top_n (int): Number of top keywords to return
Returns:
Dict: Basic keyword analysis
"""
if not text_content:
return {"error": "No text content to analyze"}
try:
# Basic keyword extraction using word frequency
words = re.findall(r'\b\w+\b', text_content.lower())
# Filter out common stop words
stop_words = {
'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by',
'is', 'are', 'was', 'were', 'be', 'been', 'have', 'has', 'had', 'do', 'does', 'did',
'will', 'would', 'could', 'should', 'may', 'might', 'can', 'this', 'that', 'these', 'those',
'i', 'you', 'he', 'she', 'it', 'we', 'they', 'me', 'him', 'her', 'us', 'them'
}
# Filter words and count frequency
filtered_words = [word for word in words if len(word) > 2 and word not in stop_words]
word_freq = {}
for word in filtered_words:
word_freq[word] = word_freq.get(word, 0) + 1
# Get top keywords
top_keywords = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:top_n]
# Extract potential phrases (2-3 word combinations)
phrases = []
words_list = text_content.lower().split()
for i in range(len(words_list) - 1):
if len(words_list[i]) > 2 and len(words_list[i+1]) > 2:
phrase = f"{words_list[i]} {words_list[i+1]}"
if not any(stop in phrase for stop in ['the ', 'and ', 'or ', 'but ']):
phrases.append(phrase)
phrase_freq = {}
for phrase in phrases:
phrase_freq[phrase] = phrase_freq.get(phrase, 0) + 1
top_phrases = sorted(phrase_freq.items(), key=lambda x: x[1], reverse=True)[:5]
return {
"total_words": len(words),
"unique_words": len(set(words)),
"vocabulary_diversity": len(set(words)) / len(words) if words else 0,
"top_keywords": [{"word": word, "frequency": freq} for word, freq in top_keywords],
"top_phrases": [{"phrase": phrase, "frequency": freq} for phrase, freq in top_phrases],
"word_frequency_distribution": dict(top_keywords)
}
except Exception as e:
return {"error": f"Keyword analysis failed: {str(e)}"}
class AnalysisOrchestrator:
"""Orchestrates all analysis components."""
def __init__(self):
self.scraper = WebScraper()
self.seo_analyzer = SEOAnalyzer()
self.readability_analyzer = ReadabilityAnalyzer()
self.content_analyzer = ContentAnalyzer()
self.keyword_analyzer = KeywordAnalyzer()
def analyze_url_comprehensive(self, url: str) -> Dict:
"""
Performs comprehensive analysis using all available components.
Args:
url (str): The URL to analyze
Returns:
Dict: Comprehensive analysis results
"""
# 1. Scrape the URL
scraped_data = self.scraper.scrape_url(url)
if scraped_data["status"] != "success":
return {
"url": url,
"status": "failed",
"error": scraped_data.get("error", "Scraping failed"),
"analysis": {}
}
text_content = scraped_data.get("text_content", "")
# 2. Run all analyses
analyses = {}
# SEO Analysis
analyses["seo_metrics"] = self.seo_analyzer.analyze_seo_metrics(scraped_data, url)
# Readability Analysis
analyses["readability_metrics"] = self.readability_analyzer.analyze_readability(text_content)
# Content Structure Analysis
analyses["content_structure"] = self.content_analyzer.analyze_content_structure(text_content, scraped_data)
# Keyword Analysis
analyses["keyword_analysis"] = self.keyword_analyzer.extract_basic_keywords(text_content)
# Basic content metadata
analyses["content_metadata"] = {
"content_length": len(text_content),
"has_content": len(text_content) > 100,
"language_detected": "en", # Could be enhanced with language detection
"analysis_timestamp": None # Could add timestamp
}
return {
"url": url,
"status": "success",
"scraped_data": {
"meta_title": scraped_data.get("meta_title"),
"meta_description": scraped_data.get("meta_description"),
"content_length": scraped_data.get("content_length")
},
"analysis": analyses
} |