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

2024-06-16 14:42:56 发布

您现在位置: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']

我希望将其转换为链接形式并使用代码


    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

怎么解决呢?


Tags: of字符串httpsurl列表xlspartp2000
2条回答

我想你不需要引用。它应该使用以下代码。如果没有,请澄清并分享您的完整来源

    for urlValue in url:
        print(urlValue)

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

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

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

相关问题 更多 >