且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Python 将字符串列表转换为 URL 列表

更新时间:2022-10-26 10:56:05

"quote";这是错误的方法.它专门用于从字符串中删除特殊字符,这就是您所看到的.

如果您正在尝试构建 url 对象,urllib.parse.urlparse(urlValue) 将是您想要的方法.

作为 Python 的新手 - urllib 库的 API 非常低.假设您正在尝试建立网络连接,我强烈建议您查看请求"部分.pypi 的库,这是一个更简单的用户界面.

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?

"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.