Nguyễn Nam

Trong SEO, việc phân tích nội dung trang web giúp đánh giá mức độ liên quan của bài viết với từ khóa và lĩnh vực cụ thể. Một cách hữu ích để thực hiện điều này là sử dụng Named Entity Recognition (NER) để trích xuất các thực thể quan trọng từ nội dung trang web. Ngoài ra, việc phân tích cảm xúc (Sentiment Analysis) giúp xác định giọng điệu của nội dung (tích cực, tiêu cực hay trung lập).

Trong bài viết này, chúng ta sẽ sử dụng Python để tự động hóa quá trình trên bằng cách thu thập dữ liệu từ URL, thực hiện phân tích cảm xúc bằng TextBlob và trích xuất các thực thể quan trọng bằng Spacy.


1. Cài Đặt Thư Viện Cần Thiết

Trước khi chạy mã trong Google Colab, bạn cần cài đặt các thư viện Python sau bằng cách chạy lệnh trong một ô code:

!pip install requests beautifulsoup4 textblob spacy
!python -m spacy download en_core_web_sm

2. Giải Thích Code

Dưới đây là đoạn code Python hoàn chỉnh để phân tích cảm xúc và trích xuất các thực thể quan trọng từ nội dung của một trang web.

import requests
from bs4 import BeautifulSoup
from textblob import TextBlob
import spacy
from collections import Counter

def fetch_webpage_content(url):
    """Lấy nội dung trang web từ URL."""
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    text_elements = soup.find_all('p') + soup.find_all('h1') + soup.find_all('h2') + soup.find_all('h3') + soup.find_all('h4')
    return ' '.join([p.text for p in text_elements])

def analyze_sentiment(text):
    """Phân tích cảm xúc của văn bản."""
    blob = TextBlob(text)
    return blob.sentiment.polarity, blob.sentiment.subjectivity

def extract_and_rank_entities(text):
    """Nhận diện và xếp hạng các thực thể được nhắc đến nhiều nhất."""
    nlp = spacy.load('en_core_web_sm')
    doc = nlp(text)
    entities = [ent.text for ent in doc.ents]
    entity_counts = Counter(entities)
    ranked_entities = [(entity, entity_counts[entity], doc.ents[entities.index(entity)].label_)
                       for entity in sorted(set(entities), key=lambda x: entity_counts[x], reverse=True)]
    return ranked_entities

def analyze_webpage(url):
    text = fetch_webpage_content(url)
    polarity, subjectivity = analyze_sentiment(text)
    ranked_entities = extract_and_rank_entities(text)
    return {
        "sentiment": {
            "polarity": polarity,
            "subjectivity": subjectivity,
        },
        "entities": ranked_entities
    }

# Nhập URL từ người dùng
url = input("Nhập URL của trang web cần phân tích: ")
results = analyze_webpage(url)

# Hiển thị kết quả
print("\nKết quả phân tích cảm xúc:")
print(f"Polarity: {results['sentiment']['polarity']}")
print(f"Subjectivity: {results['sentiment']['subjectivity']}")

print("\nCác thực thể được nhận diện:")
for entity, count, label in results['entities']:
    print(f"Thực thể: {entity}, Xuất hiện: {count} lần, Loại: {label}")

4. Ứng Dụng Thực Tế Trong SEO


Kết Luận

Bằng cách sử dụng Python trong Google Colab, bạn có thể tự động hóa phân tích nội dung web, giúp cải thiện chiến lược SEO hiệu quả hơn. Công cụ này đặc biệt hữu ích cho nghiên cứu từ khóa, đánh giá nội dung đối thủ và tối ưu hóa bài viết.

16 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin