17 thg 4, 2026·Proxy Dân Cư·3 min read

Web Scraping Python Với Proxy Dân Cư: Hướng Dẫn Từ Cơ Bản Đến Nâng Cao

N
NestProxy
Web Scraping Python Với Proxy Dân Cư: Hướng Dẫn Từ Cơ Bản Đến Nâng Cao

Web scraping là kỹ năng cốt lõi của data engineer, researcher và marketer 2026. Nhưng scraping mà không có proxy thì giống như đi câu cá mà không có mồi — thất bại chỉ là vấn đề thời gian. Bài viết này hướng dẫn web scraping với Python kết hợp proxy dân cư NestProxy.


Tại Sao Web Scraping Cần Proxy?

Vấn đềKhông proxyCó proxy dân cư
IP bị blockSau 50-100 requestScraping hàng ngàn trang
Rate limitingBị throttle ngayXoay IP → bypass limit
CAPTCHAHiện liên tụcGiảm 90% CAPTCHA
Geo-restrictionChỉ thấy 1 vùngScraping 63 tỉnh/thành

Setup Môi Trường

Cài đặt thư viện

# Cài đặt các thư viện cần thiết
pip install requests beautifulsoup4 playwright httpx

# Cài browser cho Playwright
playwright install chromium

Phương Pháp 1: Requests + BeautifulSoup (Nhanh, Nhẹ)

Phù hợp cho website tĩnh (không cần render JavaScript):

import requests
from bs4 import BeautifulSoup
import random
import time

# Pool proxy NestProxy
proxies_list = [
    {"http": "http://key1:pass@proxy1.nestproxy.com:PORT"},
    {"http": "http://key2:pass@proxy2.nestproxy.com:PORT"},
    {"http": "http://key3:pass@proxy3.nestproxy.com:PORT"},
]

user_agents = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64)...",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...",
]

def scrape_with_proxy(url):
    proxy = random.choice(proxies_list)
    headers = {"User-Agent": random.choice(user_agents)}
    
    try:
        resp = requests.get(url, proxies=proxy, headers=headers, timeout=10)
        soup = BeautifulSoup(resp.text, "html.parser")
        return soup
    except Exception as e:
        print(f"Error: {e}, rotating proxy...")
        return scrape_with_proxy(url)  # Retry with new proxy

# Sử dụng
soup = scrape_with_proxy("https://example.com/products")
products = soup.find_all("div", class_="product-item")
for p in products:
    print(p.find("span", class_="price").text)

Phương Pháp 2: Playwright (Website Động, JavaScript)

Phù hợp cho Shopee, Lazada, TikTok — cần render JavaScript:

from playwright.sync_api import sync_playwright
import random

def scrape_dynamic_site(url, proxy_config):
    with sync_playwright() as p:
        browser = p.chromium.launch(
            proxy={
                "server": proxy_config["server"],
                "username": proxy_config["username"],
                "password": proxy_config["password"],
            },
            headless=True
        )
        context = browser.new_context(
            user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64)..."
        )
        page = context.new_page()
        
        # Block images to speed up
        page.route("**/*.{png,jpg,jpeg,gif,svg}", lambda r: r.abort())
        
        page.goto(url, wait_until="networkidle")
        
        # Random delay giữa các thao tác
        page.wait_for_timeout(random.randint(1000, 3000))
        
        content = page.content()
        browser.close()
        return content

# Cấu hình NestProxy
proxy = {
    "server": "http://proxy.nestproxy.com:PORT",
    "username": "YOUR_KEY",
    "password": "YOUR_PASSWORD"
}

html = scrape_dynamic_site("https://shopee.vn/search?keyword=iphone", proxy)

Phương Pháp 3: httpx Async (Tốc Độ Cao)

Scraping hàng ngàn trang đồng thời:

import httpx
import asyncio

async def scrape_url(client, url):
    try:
        resp = await client.get(url, timeout=10)
        return {"url": url, "status": resp.status_code, "data": resp.text[:500]}
    except:
        return {"url": url, "status": "error"}

async def main():
    proxy = "http://YOUR_KEY:pass@proxy.nestproxy.com:PORT"
    
    async with httpx.AsyncClient(proxy=proxy) as client:
        urls = [f"https://example.com/product/{i}" for i in range(1, 1001)]
        tasks = [scrape_url(client, url) for url in urls]
        results = await asyncio.gather(*tasks)
        
        success = [r for r in results if r["status"] == 200]
        print(f"Success: {len(success)}/{len(urls)}")

asyncio.run(main())

Best Practices Anti-Detection 2026

  1. Random delay: 2-5 giây giữa request (không đều đặn)
  2. Xoay User-Agent: Pool 10+ UA strings khác nhau
  3. Block resources: Tắt ảnh, CSS, font để tăng tốc
  4. Fingerprint spoofing: Dùng playwright-stealth
  5. Retry logic: Exponential backoff khi gặp 403/429
  6. Respect robots.txt: Tuân thủ quy định website
  7. Session management: Sticky session cho login flows

NestProxy + Python = Scraping Không Giới Hạn

  • ✅ IP dân cư 63 tỉnh — bypass mọi anti-bot
  • ✅ API đổi IP — tích hợp vào code Python
  • ✅ HTTP + SOCKS5 — tương thích requests, Playwright, httpx
  • ✅ Bandwidth không giới hạn — scraping thoải mái
  • ✅ MCP cho AI Agent — scraping thông minh

👉 Bắt đầu scraping: nestproxy.com

Bài viết liên quan

Rotating vs Static vs Sticky Proxy: Khi Nào Dùng Loại Nào?
17 thg 4, 2026·Proxy Dân Cư·1 min read

Rotating vs Static vs Sticky Proxy: Khi Nào Dùng Loại Nào?

So sánh Rotating vs Static vs Sticky Session Proxy. Bảng phân tích chi tiết, khi nào dùng loại nào, chiến lược kết hợp cho scraping, nuôi nick, SEO.

N
NestProxy
VPN vs Proxy: Khác Nhau Gì? Khi Nào Dùng Cái Nào? So Sánh Chi Tiết 2026
17 thg 4, 2026·Proxy Dân Cư·1 min read

VPN vs Proxy: Khác Nhau Gì? Khi Nào Dùng Cái Nào? So Sánh Chi Tiết 2026

So sánh chi tiết VPN vs Proxy: khác nhau gì, khi nào dùng cái nào. Tại sao VPN không thể thay thế proxy cho nuôi nick, scraping, MMO. Bảng quyết định chọn công cụ phù hợp.

N
NestProxy