You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

无法通过Python Requests/Selenium获取目标网站响应,求排查方案

解决无法获取MSC Direct产品页面响应的问题

看起来你是被网站的反爬机制拦住了——这类电商平台通常有严格的反爬策略,哪怕用了Session或者Selenium,没做针对性配置也容易被识别。咱们一步步排查你可能遗漏的关键点:

一、Requests库的优化方向

你提到已经加了请求头,但可能字段不够完整,这类网站会校验很多细节:

  • 补全关键请求头:必须模拟真实浏览器的请求标识,比如User-Agent(用Chrome最新版本的标识)、AcceptAccept-LanguageReferer(设为MSC首页)、Upgrade-Insecure-Requests
  • 先拿首页Cookie:有些网站会在首次访问首页时生成必要的会话Cookie,直接请求产品页会被拦截
  • 尝试代理IP:如果你的IP被网站标记为爬虫,换个代理试试
  • 检查动态加载:产品数据可能是通过Ajax接口异步加载的,打开浏览器开发者工具的Network面板,看看XHR请求里有没有真正的数据源接口

优化后的Requests示例代码:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Referer': 'https://www.mscdirect.com/',
    'Upgrade-Insecure-Requests': '1'
}

session = requests.session()
# 先访问首页获取会话Cookie
session.get('https://www.mscdirect.com/', headers=headers)
# 再请求目标产品页
response = session.get('https://www.mscdirect.com/product/details/49784960', headers=headers)
print(response.status_code)  # 先看状态码,200才是正常响应
print(response.text)

二、Selenium的反识别优化

Selenium自带的特征很容易被网站检测到,需要做针对性配置:

  • 完善无头模式参数:启用无头时要加更多模拟真实浏览器的配置
  • 隐藏自动化标识:网站会通过window.navigator.webdriver判断是否是爬虫,需要手动修改这个属性
  • 添加用户代理:避免使用Selenium默认的UA
  • 增加等待时间:模拟人类浏览的节奏,不要瞬间跳转页面
  • 试试undetected-chromedriver:这是专门绕过反爬的Selenium封装,会自动处理大部分检测逻辑

优化后的标准Selenium代码:

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

options = Options()
# 无头模式的新写法(Chrome 112+)
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
# 禁用自动化特征检测
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 设置真实用户代理
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")

driver = webdriver.Chrome(options=options)
# 执行脚本隐藏webdriver属性
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.delete_all_cookies()
# 先访问首页建立会话
driver.get("https://www.mscdirect.com/")
time.sleep(2)
# 再跳转产品页
driver.get("https://www.mscdirect.com/product/details/49784960")
time.sleep(3)  # 等待页面完全加载
print(driver.page_source)
driver.quit()

如果上面的配置还是不行,推荐用undetected-chromedriver,安装命令:pip install undetected-chromedriver,使用示例:

import undetected_chromedriver as uc
import time

driver = uc.Chrome()
driver.get("https://www.mscdirect.com/product/details/49784960")
time.sleep(3)
print(driver.page_source)
driver.quit()

三、其他要注意的点

  • Cloudflare验证:如果访问时出现人机验证页面,普通的爬虫工具绕不过,可能需要借助验证码服务(比如2Captcha)来处理(涉及付费)
  • 状态码排查:如果返回403、429,说明被拒绝访问;如果返回200但内容为空,大概率是反爬机制返回的空白页面

内容的提问来源于stack exchange,提问作者sam mathew

火山引擎 最新活动