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

如何用BeautifulSoup绕过ArcGIS网站条款协议弹窗爬取项目名称?

解决ArcGIS网站条款弹窗爬取问题

方案一:用Selenium模拟浏览器交互(适合新手)

该网站的条款弹窗是前端JavaScript渲染的,requests仅能获取静态HTML,无法处理用户交互,因此可以用Selenium模拟真人操作绕过弹窗:

  1. 先安装依赖:
pip install selenium

同时下载对应浏览器的驱动(比如ChromeDriver,需与你的浏览器版本匹配),可放到Python可执行路径或在代码中指定路径。

  1. 示例代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 初始化Chrome浏览器驱动
driver = webdriver.Chrome()
try:
    # 打开目标网站
    driver.get("https://brighton.maps.arcgis.com/apps/webappviewer/index.html?id=2e3dacc6615e4cf59b6db043cc3f12cc")
    
    # 等待条款弹窗加载,勾选同意复选框
    checkbox = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='checkbox']"))
    )
    checkbox.click()
    
    # 点击同意按钮
    agree_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-action='accept']"))
    )
    agree_btn.click()
    
    # 页面加载完成后,可根据实际页面结构定位项目元素提取名称
    # 示例(需根据实际调整选择器):
    # projects = driver.find_elements(By.CSS_SELECTOR, ".project-item-title")
    # for p in projects:
    #     print(p.text)
    
finally:
    # 关闭浏览器
    driver.quit()

方案二:直接调用ArcGIS后台API(更高效)

ArcGIS Web App通常通过后台API加载数据,你可以绕过前端弹窗,直接请求数据接口:

  1. 打开目标网站,按F12打开开发者工具,切换到「网络」标签。
  2. 勾选条款同意后刷新页面,查找包含项目数据的API请求(通常是GET请求,路径含/rest/services/或/features/)。
  3. 复制该API的URL,用requests直接请求,解析返回的JSON数据即可得到项目名称。

示例代码(需替换为你找到的实际API地址):

import requests

# 替换为你通过开发者工具找到的API URL
API_URL = "https://brighton.maps.arcgis.com/sharing/rest/content/items/2e3dacc6615e4cf59b6db043cc3f12cc/data"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}

response = requests.get(API_URL, headers=headers)
data = response.json()

# 根据返回的JSON结构提取项目名称,示例(需根据实际调整):
# for project in data["operationalLayers"][0]["featureCollection"]["layers"][0]["featureSet"]["features"]:
#     print(project["attributes"]["ProjectName"])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 10:16:10