export content.search results to excel

499
5
03-05-2020 11:44 AM
LouisK_
New Contributor

how do i export the results of the search to excel?  In the export i would want username, modified date, created date and view count

import arcgis
from arcgis.gis import GIS


portal = "https://xxxxx"


gis = GIS(portal, "username", "password", verify_cert=False)
print ("Connected")

items = gis.content.search(query="owner: s*",item_type='', sort_field="numViews", sort_order= "asc", max_items=5000, outside_org=False)
for item in items:
display(item)

Reply
0 Kudos
5 Replies
MehdiPira1
Esri Contributor

Hi Louis K.

Here's the script that does what you are after:

from arcgis.gis import GIS
from arcgis.features import SpatialDataFrame
import pandas as pd

gis = GIS(portal, "username", "password", verify_cert=False)
print("Connected")

search_result = gis.content.search(query="owner: s*", item_type="Feature Service", sort_field="numViews", sort_order="asc", max_items=5000)
df_list = []
for item in search_result:
items = item.items()
df = pd.DataFrame.from_dict(items)
dfTransposed = df.T
dfTransposed.columns = dfTransposed.iloc[0]
dfTransposed = dfTransposed[1:]
dfTransposed = dfTransposed.set_index('name', inplace=False)
dfTransposed = dfTransposed[["owner", "created", "modified", "numViews"]]
df_list.append(dfTransposed)
merged_df = pd.concat(df_list)
# export to excel
merged_df.to_excel(r"Path to output.xlsx")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Just input your destination path for the excel output to the script.

AdityarajChavada1
New Contributor II

Is there a way to convert the date format (e.g. 1570463334131) to more readable when exporting into excel file?

Reply
0 Kudos
MinbinJiang
Esri Contributor

I believe this can be done by python DateTime object converting timestamp to date format. 

MehdiPira1
Esri Contributor

Adityaraj Chavada

The function below does the trick:

import datetime

def modifiedTime(index, dataframe):
ts = dataframe.iloc[index].created/1000
convertedTime = datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
return convertedTime‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This can be included in the for loop to convert the unix time. This is for the 'created' column.

Same can be applied to the 'modified' column too.

AdityarajChavada1
New Contributor II

Thanks, Mehdi Pira‌! I am no expert at ArcGIS API for Python. Forgive me for silly question. Can you confirm where exactly I need to insert this function in the for loop and how I can get the updated date printed in the excel file? Thank you so much for your help..

Reply
0 Kudos