Việc lưu trữ và truyền tải nội dung trang web có thể tiêu tốn nhiều dung lượng, đặc biệt với các trang có nhiều văn bản. Nén dữ liệu giúp giảm băng thông và chi phí lưu trữ, đồng thời tăng tốc độ tải trang.
Bài viết này hướng dẫn bạn cách sử dụng Python để trích xuất nội dung từ một URL, nén dữ liệu bằng Gzip, và tính toán tỷ lệ nén để ước lượng chi phí lưu trữ.
1. Cài Đặt Thư Viện Cần Thiết
Trước khi bắt đầu, bạn cần cài đặt các thư viện cần thiết:
!pip install requests beautifulsoup4
Các thư viện chính được sử dụng:
- requests: Dùng để gửi HTTP request và lấy nội dung trang web.
- BeautifulSoup: Hỗ trợ phân tích cú pháp HTML và trích xuất nội dung.
- gzip: Sử dụng để nén dữ liệu văn bản.
2. Lấy Nội Dung Từ Trang Web
Dưới đây là cách lấy và làm sạch nội dung từ một trang web, đồng thời loại bỏ các phần không cần thiết như script, style, header, footer.
import requests
from bs4 import BeautifulSoup
def fetch_and_parse(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
with requests.Session() as session:
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
# Loại bỏ các phần không cần thiết
for tag in soup(['head', 'header', 'footer', 'script', 'style', 'meta']):
tag.decompose()
return soup
3. Trích Xuất Nội Dung Có Chọn Lọc
Dữ liệu sẽ được trích xuất từ các thẻ p, li, h1-h6, table, đảm bảo giữ lại những phần quan trọng nhất của bài viết.
import re
def extract_text_selectively(soup):
individual_tags = {'p', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'table', 'tr'}
container_tags = {'div', 'section', 'article', 'main'}
excluded_tags = {'style', 'script', 'meta', 'body', 'html', '[document]', 'button'}
inline_tags = {'a', 'strong', 'b', 'i', 'em', 'span'}
text_lines = []
processed_elements = set()
for element in soup.find_all(True, recursive=True):
if element in processed_elements or element.name in excluded_tags:
continue
if element.name in individual_tags:
inline_text = ' '.join(element.stripped_strings)
if inline_text and element not in processed_elements:
text_lines.append(inline_text)
processed_elements.add(element)
combined_text = ' '.join(text_lines)
return combined_text
4. Nén Dữ Liệu Và Tính Tỷ Lệ Nén
Sau khi thu thập nội dung, chúng ta sẽ sử dụng Gzip để nén dữ liệu và tính toán tỷ lệ nén để đánh giá mức độ tiết kiệm dung lượng.
import gzip
def calculate_compression_ratio(text):
original_size = len(text.encode('utf-8'))
compressed_data = gzip.compress(text.encode('utf-8'))
compressed_size = len(compressed_data)
compression_ratio = original_size / compressed_size
return compression_ratio
5. Thực Thi Chương Trình
Chạy đoạn mã dưới đây để nhập URL, trích xuất nội dung, nén và hiển thị kết quả tỷ lệ nén.
# Nhập URL cần phân tích
url = input("Please enter the URL: ")
# Lấy nội dung trang web
soup = fetch_and_parse(url)
combined_text = extract_text_selectively(soup)
# Hiển thị nội dung đã trích xuất
print(f"\nExtracted text from the page:\n{combined_text[:1000]}...") # Giới hạn hiển thị 1000 ký tự đầu tiên
# Nén dữ liệu và tính toán tỷ lệ
print("Compressing text...")
compression_ratio = calculate_compression_ratio(combined_text)
print(f"\nCompression ratio for {url}: {compression_ratio:.2f}")
6. Ứng Dụng Thực Tế
Tiết Kiệm Chi Phí Lưu Trữ
Tỷ lệ nén giúp bạn ước tính không gian lưu trữ cần thiết cho nội dung trang web:
- Dung lượng gốc: 1GB nội dung gốc
- Tỷ lệ nén ~2.5 → Dung lượng sau nén ≈ 400MB
Tăng Hiệu Suất Tải Trang
Nén nội dung giúp trang tải nhanh hơn, giảm độ trễ và cải thiện trải nghiệm người dùng.
Ứng Dụng Trong SEO
Google đánh giá cao các trang có tốc độ tải nhanh, giúp tăng Core Web Vitals và xếp hạng cao hơn trên công cụ tìm kiếm.
Kết Luận
Bằng cách sử dụng Python, BeautifulSoup và Gzip, bạn có thể tự động thu thập nội dung trang web, nén dữ liệu và tính toán chi phí lưu trữ một cách hiệu quả.
https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=4808
https://vitz.ru/forums/index.php?autocom=gallery&req=si&img=4847
Very good https://is.gd/tpjNyL
Very good https://is.gd/tpjNyL
Good https://shorturl.at/2breu
https://myteana.ru/forums/index.php?autocom=gallery&req=si&img=6917
https://hrv-club.ru/forums/index.php?autocom=gallery&req=si&img=7165
https://shorturl.fm/1XhN3
https://shorturl.fm/MhPe7
https://shorturl.fm/1TsgB
https://shorturl.fm/9GgYB
https://shorturl.fm/IvaIT
https://shorturl.fm/pAWvf
https://shorturl.fm/Wwx1D
https://shorturl.fm/cZu07
https://shorturl.fm/J3kJA
https://shorturl.fm/k291H
https://shorturl.fm/zHzTE
https://shorturl.fm/NJAWS
https://shorturl.fm/ocyBJ
https://shorturl.fm/RNeKx
https://shorturl.fm/csH8h
https://shorturl.fm/EW46G
https://shorturl.fm/x7xVU
https://shorturl.fm/6hW8t
https://shorturl.fm/Tl1Ky
https://shorturl.fm/XU6fH
https://shorturl.fm/25XNL
https://shorturl.fm/O5fGr
https://shorturl.fm/RFZCJ
https://shorturl.fm/hoQRo
https://shorturl.fm/mDhLk
https://shorturl.fm/HCMkD
https://shorturl.fm/awCKO
https://shorturl.fm/WjZ5Q
https://shorturl.fm/MH1yh
https://shorturl.fm/JDjeH
https://shorturl.fm/QNf1O
https://shorturl.fm/kjTMB
https://shorturl.fm/alTwT
https://shorturl.fm/hENaA
https://shorturl.fm/uxwbp
https://shorturl.fm/vvXWB
https://shorturl.fm/ZaZYB
https://shorturl.fm/kuuZl
https://shorturl.fm/egiom
https://shorturl.fm/pJZ35
https://shorturl.fm/AevMN
https://shorturl.fm/8WmGm
https://shorturl.fm/gO6r9
https://shorturl.fm/eFICz
https://shorturl.fm/ZTV2m
https://shorturl.fm/UfYBE
https://shorturl.fm/wuG7T
https://shorturl.fm/YK9vR
https://shorturl.fm/9d1ys
https://shorturl.fm/LzVZx
https://shorturl.fm/jAtj9
https://shorturl.fm/tS0U7
https://shorturl.fm/X4bBn
Tap into unlimited earning potential—become our affiliate partner!
Refer friends and colleagues—get paid for every signup!
Unlock top-tier commissions—become our affiliate partner now!
Join our affiliate program today and start earning up to 30% commission—sign up now!
Monetize your audience—become an affiliate partner now!
Maximize your income with our high-converting offers—join as an affiliate!
Join our affiliate community and maximize your profits—sign up now!
Start profiting from your network—sign up today!
https://shorturl.fm/bPz2k
https://shorturl.fm/7S4cK
https://shorturl.fm/DIKqv
https://shorturl.fm/L1102
https://shorturl.fm/lM0ee
https://shorturl.fm/avElV
https://shorturl.fm/1X6lI
https://shorturl.fm/rBwgm
https://shorturl.fm/bR4GR
https://shorturl.fm/BHokQ
https://shorturl.fm/uYszL
https://shorturl.fm/jG0zB
Great article, thanks for sharing this insight.
Everything from the layout to the game selection is top-notch. I feel very secure playing here and the rewards are generous. Go check pknb999 right now.
I’ve tried a lot of online slots but this one really hits different. The payouts are fair and the interface is super smooth. Definitely my new favorite spot to chill and win! jollibe777slot