使用 Python 调用 Bright Data MCP Server 实现实时网页数据抓取

环境准备

确保已安装 Python 3.7+ 和 VS Code。安装必要的库:

pip install requests selenium webdriver-manager

配置 Bright Data MCP
  1. 登录 Bright Data 控制台,创建 MCP(Mobile Carrier Proxy)服务器。
  2. 获取 MCP 服务器的访问凭据(主机、端口、用户名、密码)。
  3. 根据目标网站需求选择移动运营商和地理位置。
Python 实现代码

以下示例使用 requests 库通过 MCP 代理抓取数据:

import requests

# Bright Data MCP 代理配置
proxy_host = "your-mcp-host.zproxy.luminati.io"
proxy_port = 22225
proxy_username = "your-username"
proxy_password = "your-password"

proxies = {
    "http": f"http://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}",
    "https": f"http://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}"
}

url = "https://example.com"

try:
    response = requests.get(url, proxies=proxies, timeout=30)
    response.raise_for_status()
    print(response.text)
except requests.exceptions.RequestException as e:
    print(f"Error: {e}")

使用 Selenium 实现动态渲染

对于需要 JavaScript 渲染的页面:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument(f"--proxy-server=http://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")
print(driver.page_source)
driver.quit()

VS Code 调试配置
  1. 创建 .vscode/launch.json 文件:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}


 

  1. 设置断点后按 F5 启动调试。
最佳实践
  • 遵守目标网站的 robots.txt 规则
  • 设置合理的请求间隔(如 time.sleep(2)
  • 使用随机 User-Agent 模拟移动设备
  • 处理 CAPTCHA 和反爬机制时考虑 Bright Data 的自动解验证功能
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐