Select to view content in your preferred language

Python code to run hyperlink in the attribute table

193
3
3 weeks ago
JonPeroff
New Contributor II

I am using arcpro 3.3 and I have an attribute table with a field with hyperlinks to download files from the internet.  Is there a tool or script to run to automatically do this?

0 Kudos
3 Replies
BobBooth1
Esri Contributor

Hi Jon,

Are you trying to download every linked item from each row of your table? Or are you trying to format hyperlinks so a pop-up will allow the user to download a selected link?

Best,

Bob

0 Kudos
JonPeroff
New Contributor II

Hi Bob,

Happy Canada Day!

I am trying to download from every linked item in a row.  The URL is in one field of the attribute table and there are many rows, too many to download one at a time.

Thanks,

Jon

0 Kudos
BobBooth1
Esri Contributor

Hi Jon,

Happy Canada Day to you!

So, to do the downloading automatically, I'd use a search cursor to iterate over the table and get the values from the URL field in a list.

https://pro.arcgis.com/en/pro-app/latest/arcpy/data-access/searchcursor-class.htm

You could make a list, my_urls, for example, and for each cycle of the search cursor append the URL string to the list.

https://www.w3schools.com/python/python_lists_add.asp

It would look something like this:

read_urls_into_list_from_search_cursor.png

import arcpy

# path to table/feature class
fc = r'C:\Geology\OH\OH_geol_poly'

my_urls = []

# Use a search cursor to iterate through the table.
# For each row, get the attribute value in a field, 
# and append it to a list.
with arcpy.da.SearchCursor(fc, 'SRC_URL') as cursor:
    for row in cursor:
        my_urls.append(row[0])

print(my_urls)

 

Once you have the URLs as a list, use Python to download them to your preferred location. There's an example here:

https://towardsdatascience.com/use-python-to-download-multiple-files-or-urls-in-parallel-1759da9d653...

Best,

Bob

0 Kudos