[{"data":1,"prerenderedAt":20},["ShallowReactive",2],{"blog:post:vi:xoay-ip-theo-vung-lanh-tho-de-tranh-chan-khi-scraping":3},{"slug":4,"lang":5,"title":6,"summary":7,"date":8,"tags":9,"tag_slugs":15,"thumbnail_url":16,"translations":17,"body":18,"asset_base":19},"xoay-ip-theo-vung-lanh-tho-de-tranh-chan-khi-scraping","vi","Xoay IP theo vùng lãnh thổ để tránh chặn khi scraping","Tìm hiểu cách dùng proxy theo địa lý để tránh bị chặn IP khi thu thập dữ liệu từ nhiều quốc gia.","2026-09-08",[10,11,12,13,14],"proxy","scraping","geo","rotate","python",[10,11,12,13,14],"https://blog-api.ro-proxy.com/api/blog/posts/xoay-ip-theo-vung-lanh-tho-de-tranh-chan-khi-scraping/thumbnail.svg?lang=vi",[5],"## Tại sao cần xoay IP theo vùng lãnh thổ\n\nKhi thu thập dữ liệu từ nhiều quốc gia, việc dùng một IP duy nhất rất dễ bị phát hiện và chặn. Các nền tảng thường kiểm tra hành vi theo địa lý, tần suất truy cập và dấu vân tay trình duyệt. Nếu bạn liên tục gửi yêu cầu từ một quốc gia nhưng nội dung bạn cần thu thập lại phân phối khắp thế giới, hệ thống sẽ nghi ngờ đây là bot.\n\n## Cách chọn proxy theo địa lý\n\nRoProxy cung cấp pool proxy rộng khắp 200+ quốc gia. Bạn chỉ cần lọc theo `country_code` để lấy danh sách proxy phù hợp. Ví dụ:\n\n```\n# Lấy proxy tại Việt Nam\nhttps://proxylist.roproxy.com/?country=VN\n```\n\n## Cấu hình xoay IP trong Python\n\n### Bước 1: Cài đặt thư viện\n\n```bash\npip install requests\n```\n\n### Bước 2: Tải danh sách proxy\n\n```python\nimport requests\nimport random\n\ndef get_proxies(country_code):\n    url = f\"https://proxylist.roproxy.com/?country={country_code}\"\n    response = requests.get(url)\n    lines = response.text.strip().split('\\n')\n    proxies = []\n    for line in lines:\n        parts = line.split(':')\n        if len(parts) == 4:\n            host, port, user, pwd = parts\n            proxy_url = f\"http://{user}:{pwd}@{host}:{port}\"\n            proxies.append(proxy_url)\n    return proxies\n```\n\n### Bước 3: Sử dụng xoay IP\n\n```python\ndef fetch_with_rotation(url, countries):\n    for country in countries:\n        try:\n            proxy_list = get_proxies(country)\n            if not proxy_list:\n                continue\n            proxy = random.choice(proxy_list)\n            proxies = {\n                'http': proxy,\n                'https': proxy\n            }\n            resp = requests.get(url, proxies=proxies, timeout=10)\n            if resp.status_code == 200:\n                return resp.text\n        except Exception as e:\n            print(f\"Lỗi ở {country}: {e}\")\n    return None\n\n# Ví dụ sử dụng\ncountries = ['VN', 'US', 'GB', 'DE', 'FR']\ncontent = fetch_with_rotation('https://example.com/data', countries)\n```\n\n## Tối ưu thời gian chờ và retry\n\nThêm cơ chế retry và delay để giảm thiểu lỗi kết nối:\n\n```python\nimport time\nfrom requests.adapters import HTTPAdapter\nfrom urllib3.util.retry import Retry\n\nsession = requests.Session()\nretry_strategy = Retry(\n    total=3,\n    backoff_factor=1,\n    status_forcelist=[429, 500, 502, 503, 504]\n)\nsession.mount('http://', HTTPAdapter(max_retries=retry_strategy))\nsession.mount('https://', HTTPAdapter(max_retries=retry_strategy))\n```\n\n## Kiểm tra hiệu quả xoay IP\n\nĐảm bảo mỗi yêu cầu dùng IP khác nhau:\n\n```python\nimport json\n\ndef check_ip():\n    try:\n        resp = session.get('https://api.ipify.org?format=json', proxies=proxies, timeout=10)\n        return resp.json().get('ip')\n    except:\n        return None\n\n# In IP hiện tại trước và sau khi đổi proxy\nprint('IP trước:', check_ip())\nprint('IP sau:', check_ip())\n```\n\n## Xử lý CAPTCHA và rate limit\n\nNếu gặp CAPTCHA, hãy chuyển sang proxy khác hoặc dùng dịch vụ giải CAPTCHA. Đối với rate limit, thêm thời gian chờ ngẫu nhiên:\n\n```python\nimport random\nimport time\n\ntime.sleep(random.uniform(1, 3))\n```\n\n## Kết luận\n\nXoay IP theo vùng lãnh thổ không chỉ giúp tránh chặn mà còn tăng độ tin cậy dữ liệu thu thập được. Kết hợp với cơ chế retry thông minh và kiểm tra IP định kỳ, bạn có thể xây dựng scraper bền vững cho mọi thị trường toàn cầu.\n","https://blog-api.ro-proxy.com/api/blog/posts/xoay-ip-theo-vung-lanh-tho-de-tranh-chan-khi-scraping/assets",1790057934082]