【发布时间】:2014-02-22 01:11:18
【问题描述】:
你将如何使用 Beautiful soup 在 python 中获取 URL 基名称?给定 url 名称作为字符串,你会怎么做?
【问题讨论】:
标签: python beautifulsoup base
你将如何使用 Beautiful soup 在 python 中获取 URL 基名称?给定 url 名称作为字符串,你会怎么做?
【问题讨论】:
标签: python beautifulsoup base
如果你的意思是基本名称,给定http://example.com/file.txt,你想要file.txt?在这种情况下,您根本不需要 Beautiful Soup。简单的字符串操作代码就可以了。
known 也是os.path.basename('http://example.com/file.txt) 会给你file.txt
【讨论】:
我会在 BeautifulSoup 上使用 urlparse 来提取 URL 片段。这是一个例子:
from urlparse import urlparse
parsedurl = urlparse('http://example.com/filename.txt')
print parsedurl.path
输出将是:
/文件名.txt
【讨论】: