0

I want to run python test.py "https://google.com" or python test.py https://google.com instead of ASKING as input... I havent succeeded to do that as newbie to python. How i can do that?

My python code is presented below: i have tried def main(url) but didnt work. Also i tried to use process under main but no luck too!

import re
import urllib


def main():
    import core.downloader as downloader
    import core.pdf as pdf

    print("Starting...\n")
    url = input("Enter the url of the PDF:")

    # Check that the URL provided by the user points to the entire document
    # and not to a specific page (e.g. https://issuu.com/user/docs/doc
    # instead of https://issuu.com/user/docs/doc/18)
    url_end = re.search(r'(.+)/\d+/?$', url)
    if url_end:
        # If there is a page number at the end of the URL
        print('The URL provided points to a specific page in the document.')
        url_without_page_number = url_end.group(1)
        print('Using the following URL instead:')
        print(url_without_page_number)
        url = url_without_page_number
    else:
        # If the URL points to the entire document, without any page number
        pass

    url_open1 = str(urllib.request.urlopen(url).read().decode("utf-8"))

    # Credits to https://txt2re.com/ for the regex (Almost all of it)
    # Sorry, I'm not lazy, but I hate making regex's
    re1 = '.*?'
    re2 = '((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*)(?:png|jpg))'

    rg = re.compile(re1+re2, re.IGNORECASE | re.DOTALL)
    m = rg.search(url_open1)
    if m:
        httpurl = m.group(1)
        print('Starting from URI: ' + httpurl)
        filelist = downloader.downloader(httpurl)
        pdf.creator(filelist)
    else:
        print("Error! No image was found")


if __name__ == '__main__':
    main()
Estatistics
  • 874
  • 9
  • 24
  • 1
    https://stackoverflow.com/questions/32673412/how-to-pass-command-line-arguments-in-python-3-x, https://stackoverflow.com/questions/32673412/how-to-pass-command-line-arguments-in-python-3-x you include an import on top like this "import sys from sys import argv" and get the argument by calling "sys.argv[1]" the 0 is for the script itself – Sebastian Zapata Jun 11 '22 at 00:48

1 Answers1

1

You can import the sys library if you want to get arguments from the user, for example:

import sys

first_file_name = sys.argv[1]
second_file_name = sys.argv[2]

Pay attention the index starts from 1 because the first argument is the name of the file you run. If you want to know more about sys and argv: How to use sys.argv in Python

Lee G
  • 84
  • 2