How can I extract URLs from a one line JSON text file using Regex in Python?

I’ve been trying to work this out, but I can only seem to get one URL to be exported to the output file.

The code I am currently using is…

import glob, re

with open('urls.txt', 'a') as output:
    for file in glob.glob('json.txt'):
        with open(file, 'r') as f:
            for line in f.readlines():
                pattern = r"(http|ftp|https)://([w_-]+(?:(?:.[w_-]+)+))([w.,@?^=%&:/~+#-]*[w@?^=%&/~+#-])"
                find = re.findall(pattern, line)
                if find:
                    try:
                        output.write(str(find[0]))
                    except UnicodeEncodeError:
                        pass

I’ve tested the Regex code and it’s able to match all the URLs, just won’t output them all to a file.

The file I’ve been trying to extract URLs containing the following…

{"items":[{"schema":"Event","source_id":"99558834","event_id":"7103414757044987314","start_time":"2022-05-30T06:37:10Z","end_time":"2022-05-30T06:37:24Z","event_type":"motion","source_type":null,"duration_ms":14400,"session_duration":14000,"state":"timed_out","had_subscription":true,"is_favorite":false,"recording_status":"ready","cv":{"person_detected":true,"stream_broken":false,"detection_type":"human","cv_triggers":null,"detection_types":[{"detection_type":"human","verified_timestamps":[1653892632153]}]},"properties":{"is_alexa":false,"is_sidewalk":false,"is_autoreply":false},"origin":null,"error_message":null,"updated_at":"2022-05-30T06:37:28.958Z","visualizations":{"cloud_media_visualization":{"schema":"CloudMediaVisualization","media":[{"schema":"Media","url":"https://filestore-086356611853-us-west-2-prod-data.s3.us-west-2.amazonaws.com/8cbfaccd-9b1a-458b-88b9-5d12976f4293.mp4?X-Amz-Security-Token=IQoJb3JpZ2luX2VjECcaCXVzLWVhc3QtMSJIMEYCIQCd%2FiqSm%2BFneYZ1sRxM1yNyc3Cr8bVV92jQRo6k%2B4A7pwIhAO4ufSc2Ol8wevIQBjAUZz%2B7%2B%2FZrSgGpNtDhBH6hWlikKtIECB8QABoMNzM0NDEwMjU5OTMxIgyxJGK4nrZlY0QIGNQqrwTjz9YEN9G7vRk%2Bu9qUDpVIrwzd2jNXuCJ92K%2BHVCpSQb8wFqg6%2Bh521Ukotxvl9HXThrBDfgK4madk3%2FJ1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3%2Faws4_request&X-Amz-Signature=b42e734ab24ce1c8057038a171c995326de1a8cf219810c33c0d0883e2ea38b2","custom_metadata":null,"is_e2ee":false,"manifest_id":null,"file_type":"VIDEO","file_family":"VIDEO","preroll_duration_ms":0,"playback_duration":14000,"source":"Apsara"},{"schema":"Media","url":"https://filestore-086356611853-us-west-2-prod-data.s3.us-west-2.amazonaws.com/b22b3c85-5de3-4e91-92b5-d91db479df55.mp4?X-Amz-Security-Token=IQoJb3JpZ2luX2VjECcaCXVzLWVhc3QtMSJIMEYCIQCd%2FiqSm%2BFneYZ1sRxM1yNyc3Cr8bVV92jQRo6k%2B4A7pwIhAO4ufSc2Ol8wevIQBjAUZz%2B7%2B%2FZrSgGpNtDhBH6hWlikKtIECB8QABoMNzM0NDEwMjU5OTMxIgyxJGK4nrZlY0QIGNQqrwTjz9YEN9G7vRk%2Bu9qUDpVIrwzd2jNXuCJ92K%2BHVCpSQb8wFqg6%2Bh521Ukotxvl9HXThrBDfgK4madk3%2FJ1Gynn3M%2BZ7MJnpLu0uA9tUperBazYvaNzPgFWBS2kWSUObSO5Jfwn6L9VoB4D%2F%2FHvOJa5pmDVXFc2s4hSkyxrXfw7W5OoBxdjKPU5TcdamZy7uJgLElZec%2F7PO99okNwIYQDS0RKKpcdZs3VbBiceXeb8ApDIcDWonMrnmz18Gz9wG%2B6ERrM6Av31UXID875c6DqfbqxCxpGpVXBlSy6jQENn%2Bl%2Bc5xewwhY4mTq90CcCZXnebCyoqkr2mt0S3lkZSBxdOI8qnoojCmg7yy%2BFII63h4NKQbEbhm2u1u%2Fb1Ar5UfD4wHzsalhZp83Xej5Lsg0uXvpRCaYoR6mQgvnmVmS1bIFe0StzTHhJHViwEb4XbSK3u5Z%2FniVcBbVKsidNN9%2FA33okRPz7FMjpEaOB3lsbeTpmBcC86GlnwFxarYEvWY6eN7uxE0pzuK2asYgat5JqaNj%2FbRMaW1hi7ivGAj9uFZjMteTdrsNAq6lbLaiL1POhB98D0eJumvA1xu%2FbxoE7VrW%2BikA2LOGwni5EAZ9LIzywxOHx9a5iiC%2BAFjwUGEzswdmzo0mAq0llNp1twfG5Bn47DHrUfF3NubD3aCA01mQ%2FSbKKBv%2BnMD6FK2yo9f8y2Ol%2F12%2FRLQMZbkA6i7TpaE7HNvj3ElWgwUp8OddeMPaD1ZQGOqgB4vDMx4xDOedv0RjNjZikdYtR2dHU3V4K9Ls2qUqF6NJ%2FrbvgwL1s4%2Bm3ZMeOUmLfJDMazkWg8jSNRfKBWFParp2R0%2Fg8TDUEOecwrbmN7cKG3vtnOpZIcFCD46bWvKm9czEun5zbNg6Q1rCLob5RTkEG6H0A729wvomQRldlb6QBtwAC0B7mfnRGgNZrEN3z0SSauZJS3mabSGhxwc0Oem6mFKK6s9Qh&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220530T225422Z&X-Amz-SignedHeaders=host&X-Amz-Expires=900&X-Amz-Credential=ASIA2V7SDHXNTJG3BKDZ%2F20220530%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=7724f211de257f1f13fb585158f0f241e47daa1f1f67a3e48527e45883889a8b","custom_metadata":null,"is_e2ee":false,"manifest_id":null,"file_type":"LQ_VIDEO","file_family":"LQ_VIDEO","preroll_duration_ms":0,"playback_duration":14400,"source":"Apsara"},{"schema":"Media","url":"https://filestore-086356611853-us-west-2-prod-data.s3.us-west-2.amazonaws.com/564fb900-0d78-4521-8a3d-b760fff7ee8d.iframe?X-Amz-Security-Token=IQoJb3JpZ2luX2VjECcaCXVzLWVhc3QtMSJIMEYCIQCd%2FiqSm%2BFneYZ1sRxM1yNyc3Cr8bVV92jQRo6k%2B4A7pwIhAO4ufSc2Ol8wevIQBjAUZz%2B7%2B%2FZrSgGpNtDhBH6hWlikKtIECB8QABoMNzM0NDEwMjU5OTMxIgyxJGK4nrZlY0QIGNQqrwTjz9YEN9G7vRk%2Bu9qUDpVIrwzd2jNXuCJ92K%2BHVCpSQb8wFqg6%2Bh521Ukotxvl9HXThr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530%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=8ccd9823cd6d2fe0e386b843a700bd05cc3a694c6986a55b75c797cbf846b7c6","custom_metadata":null,"is_e2ee":false,"manifest_id":null,"file_type":"THUMBNAIL","file_family":"THUMBNAIL","preroll_duration_ms":0,"playback_duration":14000,"source":"Apsara"}]},"local_media_visualization":{"schema":"LocalMediaVisualization","media":[]},"radar_visualization":null,"single_coordinate_visualization":null,"map_visualization":null},"device":{"id":99558834,"description":"Front","type":"cocoa_camera"},"owner_id":"71616327"}

Leave a Comment