本文将带你深入探索并实践如何从底层层面破解浏览器 navigator.webdriver
检测,结合爬虫代理等策略伪装、多线程加速等技术,在豆瓣图书搜索页面上批量采集图书评分、简介、作者等信息。文章面向初学者,采用分步教程型结构,并增设「陷阱警告」板块,帮助你规避常见误区,快速上手。
目标:
navigator.webdriver
检测。前置知识:
threading
或 concurrent.futures
)。获取代理配置:
proxy.16yun.cn
12345
your_username
your_password
navigator.webdriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def create_driver():
# 参考亿牛云爬虫代理示例
PROXY_HOST = 'proxy.16yun.cn' # 代理域名
PROXY_PORT = '12345' # 代理端口
PROXY_USER = 'your_username' # 代理用户名
PROXY_PASS = 'your_password' # 代理密码
CUSTOM_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)...' # 自定义 UA
options = Options()
# 隐藏 Selenium 特征
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
# 设置 User-Agent
options.add_argument(f'--user-agent={CUSTOM_UA}')
# 配置代理
proxy = f"{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}"
options.add_argument(f'--proxy-server=http://{proxy}')
# 可选:无头模式
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
# 在每次新页面加载前注入 JS,隐藏 navigator.webdriver 属性
driver.execute_cdp_cmd(
'Page.addScriptToEvaluateOnNewDocument',
{'source': "Object.defineProperty(navigator, 'webdriver', {get: () => undefined});"}
)
return driver
excludeSwitches
、disable-blink-features
避免基本指纹检测 Page.addScriptToEvaluateOnNewDocument
将 navigator.webdriver
强制设为 undefined
from selenium.webdriver.common.by import By
import time
def fetch_book_info(driver, book_name):
# 访问豆瓣图书搜索页面
url = f"https://book.douban.com/subject_search?search_text={book_name}"
driver.get('https://book.douban.com')
# 添加示例 Cookie(如有登录需求,可替换为实际 Cookie)
driver.add_cookie({'name': 'example_cookie', 'value': 'value123', 'domain': 'book.douban.com'})
driver.get(url)
time.sleep(2) # 等待页面加载
# 定位结果并点击第一个条目
first = driver.find_element(By.CSS_SELECTOR, '.subject-item .nbg')
first.click()
time.sleep(2)
# 提取评分、简介、作者
rating = driver.find_element(By.CSS_SELECTOR, 'strong.rating_num').text
summary = driver.find_element(By.CSS_SELECTOR, '#link-report .intro').text
author = driver.find_element(By.CSS_SELECTOR, '#info').text.split('\n')[0]
return {'name': book_name, 'rating': rating, 'summary': summary, 'author': author}
driver.add_cookie
实现 Cookie 注入,模拟登录状态或个性化请求from concurrent.futures import ThreadPoolExecutor
def main(book_list):
results = []
with ThreadPoolExecutor(max_workers=5) as executor:
# 每个线程创建独立 driver
futures = [executor.submit(lambda name: fetch_book_info(create_driver(), name), name)
for name in book_list]
for future in futures:
results.append(future.result())
for info in results:
print(info)
if __name__ == '__main__':
books = ['三体', '活着', '百年孤独', '小王子']
main(books)
ThreadPoolExecutor
并发执行,最大 5 个线程同时跑execute_script
在页面加载后才执行,JS 注入可能失效,必须用 Page.addScriptToEvaluateOnNewDocument
--proxy-server
参数需包含认证信息,否则返回 407 错误 max_workers
selenium.common.exceptions.WebDriverException: unknown error: net::ERR_PROXY_CONNECTION_FAILED
检查代理域名、端口及用户名密码是否正确。
NoSuchElementException
页面结构可能变动,需更新 CSS/XPath 选择器。
可增大 time.sleep
或改用 WebDriverWait
精准等待。
undetected-chromedriver
库优化指纹隐藏。Proxy-Tunnel
请求头动态控制代理 。通过本文的分步指导与示例代码,你已经掌握了从底层破解 navigator.webdriver
检测,并结合爬虫代理与多线程技术,在豆瓣图书页面上高效采集所需信息的完整流程。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。