Search code examples
pythonurlliburllib3

Convert list of String into list of URL using Python


I Have a list of string(which are actually URL)

['https://part.of.url/P2000-01.xls',

 'https://part.of.url/P2001-02.xls',

 'https://part.of.url/P2002-03.xls',

 'https://part.of.url/P2003-04.xls',

 'https://part.of.url/P2004-05.xls',

 'https://part.of.url/P2005-06.xls',

 'https://part.of.url/P2006-07.xls',

 'https://part.of.url/P2007-08.xls',

 'https://part.of.url/P2008-09.xls',

 'https://part.of.url/P2009-10.xls',

 'https://part.of.url/P2010-11.xls',

 'https://part.of.url/P2011-12.xls',

 'https://part.of.url/P2012-13.xls',

 'https://part.of.url/P2013-14.xls',

 'https://part.of.url/P2014-15.xls',

 'https://part.of.url/P2015-16.xls',

 'https://part.of.url/P2016-17.xls']

I wish to convert it into link form and using the code


    for urlValue in url:
        print(urllib.parse.quote(urlValue))

output I am getting is

https%3A//part.of.url/P2012-13.xls

https%3A//part.of.url/P2013-14.xls

https%3A//part.of.url/P2014-15.xls

https%3A//part.of.url/P2015-16.xls

It should be the list of URL containing list such as below.

https://part.of.url/P2012-13.xls

How can it be solved?


Solution

  • "quote" is the wrong method for this. It is used specifically to remove special characters from a string, which is what you are seeing.

    If you are trying to build url objects, urllib.parse.urlparse(urlValue) would be the method you want.

    As someone new to python- the urllib libraries are pretty low API. Assuming you're trying to make network connections, I would strongly recommend looking at the "requests" library from pypi, which is a much simpler user interface.