在使用 Selenium 或 Playwright 等浏览器自动化工具爬取数据时,经常会遇到「被检测」问题,尤其像 Amazon 这样反爬策略严密的网站。常见的检测机制之一就是检查 JavaScript 中的 navigator.webdriver
属性:
console.log(navigator.webdriver); // true:表明是自动化工具
因此,本文将带你深入了解如何在浏览器中底层修改该属性,并结合代理、Cookie、User-Agent 技术,实现一个能顺利爬取 Amazon 网站商品信息的反检测爬虫。
pip install undetected-chromedriver selenium requests
我们使用
undetected-chromedriver
代替原生 Selenium 驱动,内置多种反检测机制,更适合应对大型网站的反爬。
# 配置代理 亿牛云爬虫代理 www.16yun.cn
proxy_host = "proxy.16yun.cn"
proxy_port = "8010"
proxy_user = "16YUN"
proxy_pass = "16IP"
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
def create_stealth_driver(proxy_host, proxy_port, proxy_user, proxy_pass, user_agent, cookies):
options = Options()
options.add_argument(f"user-agent={user_agent}")
options.add_argument("--disable-blink-features=AutomationControlled")
# 配置爬虫代理
options.add_argument(f'--proxy-server=http://{proxy_user}:{proxy_pass}@{proxy_host}:{proxy_port}')
# 启动无头浏览器(也可以关闭无头方便调试)
# options.add_argument('--headless')
# 创建驱动
import undetected_chromedriver as uc
driver = uc.Chrome(options=options)
# 修改 webdriver 属性(核心)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
# 设置 cookie
driver.get("https://www.amazon.com")
for cookie in cookies:
driver.add_cookie(cookie)
return driver
def scrape_amazon(keyword):
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
cookies = [] # 可以从浏览器复制一组,也可通过登录获取
driver = create_stealth_driver(proxy_host, proxy_port, proxy_user, proxy_pass, user_agent, cookies)
driver.get(f"https://www.amazon.com/s?k={keyword}")
time.sleep(3)
products = driver.find_elements(By.XPATH, "//div[@data-component-type='s-search-result']")
for product in products[:10]: # 只取前10条数据举例
try:
title = product.find_element(By.TAG_NAME, "h2").text
price_whole = product.find_element(By.CLASS_NAME, "a-price-whole").text
price_frac = product.find_element(By.CLASS_NAME, "a-price-fraction").text
price = f"{price_whole}.{price_frac}"
reviews = product.find_element(By.XPATH, ".//span[@class='a-size-base']").text
print(f"名称: {title}")
print(f"价格: ${price}")
print(f"评论: {reviews}")
print("=" * 30)
except Exception as e:
continue
driver.quit()
# 请整合以上两个函数并在此调用
if __name__ == "__main__":
keyword = "wireless earbuds"
scrape_amazon(keyword)
错误提示 | 原因 | 解决方案 |
---|---|---|
| 驱动不匹配 | 使用 |
网页元素找不到 | 页面尚未完全加载 | 加 |
显示“访问过于频繁” | IP 被封 | 更换代理 IP,使用优质高匿代理 |
无法设置 Cookie | 页面未打开或未加载完成 | 先访问目标页面,再添加 Cookie |
本文以 Amazon 网站为例,讲解了如何通过底层 JS 技巧对抗自动化检测,关键点在于:
undetected-chromedriver
替代传统 Selenium;navigator.webdriver
属性隐藏自动化痕迹;原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有