如何解决Python爬取findhelp.org站点时的反爬拦截问题?
爬取findhelp.org分页数据的问题与解决思路
已尝试的方法及问题
方法1:requests + BeautifulSoup
import requests from bs4 import BeautifulSoup url="https://www.findhelp.org/care/support-network--san-francisco-ca?postal=94105" reqs = requests.get(url) soup = BeautifulSoup(reqs.text, 'lxml') print(soup)
问题:返回内容包含“Sorry, something about your browser or browsing activity made us think you were a robot.”,被反爬机制识别为机器人。
方法2:Selenium直接调用ChromeDriver
import selenium from selenium import webdriver import webbrowser url="https://www.findhelp.org/care/support-network--san-francisco-ca?postal=94105" PATH = r"C:\Users\Vinay Edula\Desktop\xxxxxxxx\chromedriver.exe" driver= webdriver.Chrome(PATH) driver.get(url)
问题:爬取1-2个页面后,请求被网站拦截。
方法3:webbrowser模块打开页面
import webbrowser import time url="https://www.findhelp.org/care/support-network--san-francisco-ca?postal=94105" chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' for i in range(10): url=f"https://www.findhelp.org/care/support-network--san-francisco-ca?postal=94105&cursor={i*10}&limit=10" webbrowser.get(chrome_path).open(url) time.sleep(10)
问题:能正常在Chrome中打开页面无拦截,但无法获取页面源码进行数据提取。
可行解决方案
针对网站反爬逻辑(对自动化工具识别严格,但允许正常浏览器访问),提供以下三种解决思路:
方案1:优化Selenium配置,模拟真实浏览器行为
直接使用Selenium被拦截,是因为默认配置带有明显自动化特征,通过以下优化可绕过检测:
from selenium import webdriver from selenium.webdriver.chrome.options import Options import time options = Options() # 隐藏自动化标识 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") # 复用本地Chrome用户数据(避免新环境被识别) options.add_argument("user-data-dir=C:/Users/你的用户名/AppData/Local/Google/Chrome/User Data") # 替换为你的Chrome用户数据路径 driver = webdriver.Chrome(options=options) # 注入脚本隐藏webdriver属性 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) """ }) # 分页爬取逻辑 for i in range(10): url = f"https://www.findhelp.org/care/support-network--san-francisco-ca?postal=94105&cursor={i*10}&limit=10" driver.get(url) time.sleep(3) # 等待页面加载完成,可替换为显式等待 # 获取页面源码用于解析 page_source = driver.page_source # 此处可添加BeautifulSoup解析逻辑 print(page_source[:500]) driver.quit()
关键优化点:
- 隐藏
navigator.webdriver属性,避免被网站检测 - 复用本地Chrome用户环境,降低被识别概率
- 添加合理等待时间,模拟用户浏览节奏
方案2:结合webbrowser与PyAutoGUI提取页面内容
利用webbrowser能正常打开页面的特性,用PyAutoGUI模拟手动操作复制页面内容:
import webbrowser import time import pyautogui chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s' for i in range(10): url = f"https://www.findhelp.org/care/support-network--san-francisco-ca?postal=94105&cursor={i*10}&limit=10" webbrowser.get(chrome_path).open(url) time.sleep(5) # 等待页面完全加载 # 模拟全选+复制操作 pyautogui.hotkey('ctrl', 'a') time.sleep(0.5) pyautogui.hotkey('ctrl', 'c') time.sleep(0.5) # 从剪贴板获取页面内容 page_content = pyautogui.paste() # 保存到本地文件 with open(f'page_{i}.html', 'w', encoding='utf-8') as f: f.write(page_content) # 关闭当前标签页 pyautogui.hotkey('ctrl', 'w') time.sleep(2)
注意事项:
- 运行时需保持Chrome为当前活动窗口
- 需提前安装PyAutoGUI:
pip install pyautogui - 等待时间需根据网络情况调整
方案3:直接调用站点API接口(最高效)
多数动态加载站点会通过API返回数据,可通过浏览器开发者工具获取接口信息:
- 打开目标页面,按F12进入开发者工具
- 切换到Network标签,刷新页面后筛选XHR类型请求
- 找到返回列表数据的API接口,复制其URL、请求头和参数
- 用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", "Cookie": "替换为浏览器中获取的Cookie" # 从开发者工具的Request Headers中复制 } for i in range(10): url = f"https://www.findhelp.org/api/v1/care/support-network?postal=94105&cursor={i*10}&limit=10" response = requests.get(url, headers=headers) data = response.json() print(data) # 直接处理结构化数据
优势:无需模拟浏览器,速度快,数据格式清晰,只要请求头模拟正确即可绕过反爬。
内容的提问来源于stack exchange,提问作者Vinay Edula
相关产品推荐
相关产品推荐

