You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python爬取Yell.com时遭遇403 Forbidden错误的解决方法

解决Yell.com爬取403 Forbidden错误的方案

核心优化方向及实现方法

  • 补全请求头字段:当前请求头缺少部分浏览器核心标识,Yell的反爬系统可能通过这些字段识别爬虫。补充后的请求头示例:
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/avif,image/webp,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-GB,en;q=0.5',
    'Referer': 'https://www.yell.com/',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1'
}
  • 轮换代理IP:你的IP大概率已被Yell封禁,使用代理池轮换IP可规避单一IP限制。requests中配置代理的方式:
proxies = {
    'http': 'http://your-proxy-ip:port',
    'https': 'https://your-proxy-ip:port'
}
response = session.get('https://www.yell.com/ucs/UcsSearchAction.do?keywords=electricians&location=London', proxies=proxies)
  • 优化Selenium配置规避检测:普通Selenium极易被识别,修改ChromeOptions隐藏自动化特征:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless=new')
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--incognito')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)

driver.get('https://www.yell.com/ucs/UcsSearchAction.do?keywords=electricians&location=London')
# 后续解析页面逻辑
  • 校验robots.txt规则:先查看Yell的robots.txt确认目标爬取路径是否被禁止,避免违反网站爬取协议。
  • 处理验证码(若触发):若网站弹出验证码,可手动处理或接入第三方识别服务,但需注意是否违反网站条款。

额外注意事项

  • 随机化请求延迟(如1-3秒随机间隔),而非固定延迟,更贴近人类浏览节奏。
  • 分批次爬取数据,避免短时间内发送大量请求。
  • 确认爬取行为符合网站服务条款,避免法律风险。

内容的提问来源于stack exchange,提问作者Ben Sogbe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 14:25:26