Pull files from a Link..Attached the source code

  • Unknown's avatar

    Hi,

    Ex: https://example-files.online-convert.com/document/txt/

    This is publically assessible link.

    Need an import option to import all text files from a link

    Sample code

    import requests
    from bs4 import BeautifulSoup
    from urllib.parse import urljoin
    
    def list_and_download_text_files(url):
        response = requests.get(url)
        if response.status_code == 200:
            soup = BeautifulSoup(response.content, 'html.parser')
            file_links = soup.find_all('a', href=True)
    
            for link in file_links:
                file_url = urljoin(url, link['href'])
                if file_url.endswith('.txt'):
                    download_text_file(file_url)
    
        else:
            print('Failed to retrieve the page.')
    
    def download_text_file(url):
        response = requests.get(url)
        if response.status_code == 200:
            file_name = url.split('/')[-1]
            with open(file_name, 'wb') as file:
                file.write(response.content)
            print(f'File {file_name} downloaded successfully.')
        else:
            print(f'Failed to download the file from {url}.')
    
    # Example usage
    url = 'https://example-files.online-convert.com/document/txt/'
    list_and_download_text_files(url)

    Thanks

  • Unknown's avatar

    Any chance of getting this added to simplenote?

  • Unknown's avatar

    This isn’t a feature we are working on adding to Simplenote at this time, but I’ve let the team know you’d like to see it.

Reply to Pull files from a Link..Attached the source code