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

Convert list of String into list of URL using Python

我有一个字符串列表(实际上是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']

我想把它转换成link形式并使用代码


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

我得到的输出是

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

应该是URL的列表,包含如下列表。

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

如何解决?

我觉得你不需要引用。它应该与以下代码一起使用。如果不是,请澄清并分享您的完整来源。

    for urlValue in url:
        print(urlValue)

“引用”是错误的方法。它专门用于从字符串中删除特殊字符,这就是您所看到的。

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

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