Python giúp tự động hóa công việc dễ dàng. Chúng ta sẽ viết script crawl tiêu đề bài viết từ trang web mẫu bằng requests và BeautifulSoup.
mport requests
from bs4 import BeautifulSoup
import csv
# URL mẫu (có thể thay bằng trang khác)
url = "https://example.com/blog"
# Gửi yêu cầu GET
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Lấy tiêu đề
titles = soup.find_all('h2', class_='post-title') # Điều chỉnh class theo trang thực tế
# Lưu vào CSV
with open('titles.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Title'])
for title in titles:
writer.writerow([title.text.strip()])
print("Đã lưu tiêu đề vào titles.csv")