浏览器头模拟:让你的爬虫更加“隐身”
## 一、为什么需要模拟浏览器头? 当你用 Python 的 `requests` 或其他爬虫库去请求网页时,默认的请求头非常简单,例如: ```http User-Agent: python-requests/2.31.0 ``` 而真实用户使用浏览器(比如 Chrome、Edge)访问网页时,请求头是复杂且丰富的,例如: ```http User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp Accept-Language: zh-CN,zh;q=0.9 Connection: keep-alive ... ``` 如果网站发现你的请求头“太机器化”,很容易判定你是爬虫并采取反制措施,比如: - 限制访问频率 - 返回假数据 - 出现验证码验证 - 直接封IP 因此,为了让你的爬虫“更像人类”,**模拟真实浏览器请求头**是非常必要的第一步! --- ## 二、常见需要模拟的HTTP头字段 | 字段 | 作用 | 示例 | | :--- | :--- | :--- | | `User-Agent` | 标识客户端浏览器 | Mozilla/5.0 (Windows NT 10.0; Win64; x64) | | `Accept` | 表示客户端支持的内容类型 | text/html,application/xhtml+xml | | `Accept-Language` | 语言偏好 | zh-CN,zh;q=0.9 | | `Referer` | 来源页面,模拟从哪个页面跳转来的 | https://www.google.com/ | | `Connection` | 是否保持连接 | keep-alive | | `Cookie` | 用户登录状态等信息 | sessionid=abc123 | 通常至少要设置 `User-Agent`,在有需要时可以加上其他字段。 --- ## 三、示例:设置自定义请求头 ### 单个静态请求示例 ```python import requests url = 'https://www.example.com/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive' } response = requests.get(url, headers=headers) print(response.status_code) print(response.text) ``` --- ### 随机切换 User-Agent(防止模式化) 可以维护一个常用浏览器 `User-Agent` 列表,每次请求随机选一个: ```python import requests import random user_agents = [ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Safari/605.1.15', 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148', ] def get_random_headers(): return { 'User-Agent': random.choice(user_agents), 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive' } url = 'https://www.example.com/' headers = get_random_headers() response = requests.get(url, headers=headers) print(response.status_code) ``` --- ## 四、进阶:使用浏览器驱动直接模拟(如 Selenium) 如果网站还对JS执行结果有校验,仅靠设置请求头可能不够,这时可以用 **Selenium**,模拟真实浏览器打开网页。 简单例子: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') # 无界面模式 chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0.0.0 Safari/537.36') driver = webdriver.Chrome(options=chrome_options) driver.get('https://www.example.com/') print(driver.page_source) driver.quit() ``` 使用 Selenium,本质上就是一台“看不见”的真浏览器,几乎不会被普通反爬识别。 --- ## 五、小技巧 - 定期更新 `User-Agent` 列表(保持与真实浏览器版本同步) - 模拟合理的 `Referer` 和 `Cookie` - 访问重要页面前,先访问首页,形成自然跳转链条 - 结合代理 IP 和请求头模拟,效果更好 --- ## 六、总结 - 单纯的 `requests` 爬虫,至少要设置合理的请求头 - 想更真实,可以结合代理 + Cookie + Referer - 如果遇到复杂反爬,考虑用 Selenium 或 Playwright 直接驱动浏览器 在实际项目中,一套成熟的爬虫通常都会有一套“随机浏览器头+代理IP+异常处理+限速策略”的组合,这样才能保证数据抓取的稳定和隐蔽性。