1

I'm trying to pass an argument in my python script as a URL. But I can't seem to find any way to allow spaces in the argument and take it as a single argument.

My command:

python -W ignore scrape.py --url 'https://www.pinterest.com/search/pins/?q=old city' --fname dat.csv

Error:

usage: scrape.py [-h] [-url URL] [-fname FNAME]
scrape.py: error: unrecognized arguments: city'

Normally, the script works if I have one word after ?q=, but I want to be able to enter multiple words after ?q=.

Tested: As seen in the above command, I tested adding quotes around my URL, but it still doesn't work.

My Python argument script:

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-url", "--url", help="Enter the URL")
    parser.add_argument("-fname", "--fname", help="Enter Filename")
    args = parser.parse_args()

    grab(args.url, args.fname)

if __name__ == "__main__":
    main()

Is there anything I can do to be able to enter a URL with spaces in the arguments?

Thanks in advance

3
  • have you tried putting double quotes instead of single quote Commented Aug 12, 2018 at 11:59
  • 4
    Which shell (or which operating system) do you use? The quotes are not interpreted by python but the shell Commented Aug 12, 2018 at 11:59
  • have you tried scrape.py --url 'https://www.pinterest.com/search/pins/?q=old%20city' --fname dat.csv and @MarkusUnterwaditzer's suggestions Commented Aug 12, 2018 at 12:03

2 Answers 2

3

Your shell (such as Windows cmd) does not interpret single quotes. You should use double quotes to quote arguments with spaces instead:

python -W ignore scrape.py --url "https://www.pinterest.com/search/pins/?q=old city" --fname dat.csv
Sign up to request clarification or add additional context in comments.

Comments

0

As stated by Anandhu, adding %20 in place of space works. There should not be any quotes around url.

So he script at url area would be like:

--url https://sitehere.com/../q=old%20place

Thanks for helping!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.