使用 Python 将字符串列表转换为 URL 列表 [英] Convert list of String into list of URL using Python

查看:69
本文介绍了使用 Python 将字符串列表转换为 URL 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<预><代码>对于 url 中的 urlValue:打印(urllib.parse.quote(urlValue))

我得到的输出是

https%3A//part.of.url/P2012-13.xlshttps%3A//part.of.url/P2013-14.xlshttps%3A//part.of.url/P2014-15.xlshttps%3A//part.of.url/P2015-16.xls

它应该是包含列表的 URL 列表,如下所示.

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

如何解决?

解决方案

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

这篇关于使用 Python 将字符串列表转换为 URL 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆