操作方法

操作方法:使用 ArcGIS API for Python 在 Esri Story Maps 或其他 web 应用程序中将 URL 从 HTTP 转换为 HTTPS

Last Published: April 25, 2020

摘要

Esri Story Maps 中的所有 URL 都必须使用 HTTPS,如 Story Maps:常见问题解答中所述。 在某些情况下,可能需要在 Story Map 或其他 web 应用程序中将 URL 从 HTTP 转换到 HTTPS。 可以手动更新 URL,但通过用户界面进行此操作十分繁琐,包含大量链接项目的 Story Maps 尤其如此。 ArcGIS API for Python 提供了执行此操作的快速工作流(适用于单个 web 应用程序)。

过程

一种方法是使用完整代码作为独立脚本将 Story Map 或其他 web 应用程序的 URL 从 HTTP 转换为 HTTPS。 为此,请将完整脚本(可在本文末尾找到)复制到文本编辑器(例如 Notepad ++)中,并将文件另存为 .py 文件。 要运行代码,请右键单击 .py 文件,然后选择使用 IDLE 编辑 > 选择运行 > 运行模块

另一种方法是使用 replace() 函数转换 URL。 为此,请执行以下步骤:
注意:两种方法都可将应用程序中的所有 URL 转换为 HTTPS。
  1. 导入所需的库。
    from arcgis.gis import GIS, Item
  2. 创建与所需 ArcGIS Online 或 Portal for ArcGIS 用户的连接。
    user = ""  # enter desired user
    password = ""  # enter password
    url = "https://www.arcgis.com"  # change Portal URL if needed
    gis = GIS(url , user, password)
    注意:要从 ArcGIS Pro 使用活动的 Portal for ArcGIS 登录帐户,如果使用企业登录帐户,请将“gis”参数替换为以下内容: 
    gis = GIS("pro")
  3. 指定所需应用程序的项目 ID。
    itemid = "" #Set the item ID 
  4. 检索应用程序数据。
    app = Item(gis, itemid)
    appdata = app.get_data(False)
  5. 运行此脚本以在应用程序数据中将 HTTP 替换为 HTTPS。
    new_appdata = appdata.replace("http://", "https://")
  6. 在步骤 5 中使用指定的 URL 更新项目,并打印结束语句。
    app.update({"text": new_appdata})
    
    print("Successfully updated item " + itemid)

    以下为完整代码:
    from arcgis.gis import GIS, Item
    
    user = "username" 
    password = "password"
    url = "https://www.arcgis.com"
    
    itemid = "abc123"
    gis = GIS(url , user, password) 
    
    app = Item(gis, itemid)
    appdata = app.get_data(False)
    
    new_appdata = appdata.replace("http://", "https://")
    
    app.update({"text": new_appdata})
    
    print("Successfully updated item " + itemid)

 

文章 ID:000018243

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

发现关于本主题的更多内容