SimpleCoder
SimpleCoder

Reputation: 67

Spaces in between user argument Python

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

Upvotes: 1

Views: 103

Answers (2)

SimpleCoder
SimpleCoder

Reputation: 67

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!

Upvotes: 0

blhsing
blhsing

Reputation: 106543

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

Upvotes: 3

Related Questions